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.
2448bbca81SDaniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
257c4ab494SYouzhong Yang * Copyright (c) 2017 by The MathWorks, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate */
274f364e7cSRobert Mustacchi /*
28ab618543SJohn Levon * Copyright 2018 Joyent, Inc.
294f364e7cSRobert Mustacchi */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include "lint.h"
327c478bd9Sstevel@tonic-gate #include "thr_uberdata.h"
33a574db85Sraf #include <pthread.h>
347c478bd9Sstevel@tonic-gate #include <procfs.h>
357c478bd9Sstevel@tonic-gate #include <sys/uio.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
37a574db85Sraf #include "libc.h"
387c478bd9Sstevel@tonic-gate
397257d1b4Sraf /*
407257d1b4Sraf * These symbols should not be exported from libc, but
417257d1b4Sraf * /lib/libm.so.2 references _thr_main. libm needs to be fixed.
427257d1b4Sraf * Also, some older versions of the Studio compiler/debugger
437257d1b4Sraf * components reference them. These need to be fixed, too.
447257d1b4Sraf */
457257d1b4Sraf #pragma weak _thr_main = thr_main
467257d1b4Sraf #pragma weak _thr_create = thr_create
477257d1b4Sraf #pragma weak _thr_join = thr_join
487257d1b4Sraf #pragma weak _thr_self = thr_self
497257d1b4Sraf
507c478bd9Sstevel@tonic-gate #undef errno
517c478bd9Sstevel@tonic-gate extern int errno;
527c478bd9Sstevel@tonic-gate
531d2738a5Sraf /*
541d2738a5Sraf * Between Solaris 2.5 and Solaris 9, __threaded was used to indicate
551d2738a5Sraf * "we are linked with libthread". The Sun Workshop 6 update 1 compilation
561d2738a5Sraf * system used it illegally (it is a consolidation private symbol).
571d2738a5Sraf * To accommodate this and possibly other abusers of the symbol,
581d2738a5Sraf * we make it always equal to 1 now that libthread has been folded
591d2738a5Sraf * into libc. The new __libc_threaded symbol is used to indicate
601d2738a5Sraf * the new meaning, "more than one thread exists".
611d2738a5Sraf */
621d2738a5Sraf int __threaded = 1; /* always equal to 1 */
631d2738a5Sraf int __libc_threaded = 0; /* zero until first thr_create() */
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate * thr_concurrency and pthread_concurrency are not used by the library.
677c478bd9Sstevel@tonic-gate * They exist solely to hold and return the values set by calls to
687c478bd9Sstevel@tonic-gate * thr_setconcurrency() and pthread_setconcurrency().
697c478bd9Sstevel@tonic-gate * Because thr_concurrency is affected by the THR_NEW_LWP flag
707c478bd9Sstevel@tonic-gate * to thr_create(), thr_concurrency is protected by link_lock.
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate static int thr_concurrency = 1;
737c478bd9Sstevel@tonic-gate static int pthread_concurrency;
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate #define HASHTBLSZ 1024 /* must be a power of two */
767c478bd9Sstevel@tonic-gate #define TIDHASH(tid, udp) (tid & (udp)->hash_mask)
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate /* initial allocation, just enough for one lwp */
797c478bd9Sstevel@tonic-gate #pragma align 64(init_hash_table)
807c478bd9Sstevel@tonic-gate thr_hash_table_t init_hash_table[1] = {
817c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, DEFAULTCV, NULL },
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate extern const Lc_interface rtld_funcs[];
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * The weak version is known to libc_db and mdb.
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate #pragma weak _uberdata = __uberdata
907c478bd9Sstevel@tonic-gate uberdata_t __uberdata = {
91e86c3f00SToomas Soome { DEFAULTMUTEX, 0, 0 }, /* link_lock */
92e86c3f00SToomas Soome { RECURSIVEMUTEX, 0, 0 }, /* ld_lock */
93e86c3f00SToomas Soome { RECURSIVEMUTEX, 0, 0 }, /* fork_lock */
94e86c3f00SToomas Soome { RECURSIVEMUTEX, 0, 0 }, /* atfork_lock */
95e86c3f00SToomas Soome { RECURSIVEMUTEX, 0, 0 }, /* callout_lock */
96e86c3f00SToomas Soome { DEFAULTMUTEX, 0, 0 }, /* tdb_hash_lock */
9736319254Sraf { 0, }, /* tdb_hash_lock_stats */
9836319254Sraf { { 0 }, }, /* siguaction[NSIG] */
997c478bd9Sstevel@tonic-gate {{ DEFAULTMUTEX, NULL, 0 }, /* bucket[NBUCKETS] */
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 { DEFAULTMUTEX, NULL, 0 }},
1097c478bd9Sstevel@tonic-gate { RECURSIVEMUTEX, NULL, NULL }, /* atexit_root */
110fc2512cfSRobert Mustacchi { RECURSIVEMUTEX, NULL }, /* quickexit_root */
1117c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, 0, 0, NULL }, /* tsd_metadata */
1127c478bd9Sstevel@tonic-gate { DEFAULTMUTEX, {0, 0}, {0, 0} }, /* tls_metadata */
1137c478bd9Sstevel@tonic-gate 0, /* primary_map */
1147c478bd9Sstevel@tonic-gate 0, /* bucket_init */
1157c478bd9Sstevel@tonic-gate 0, /* pad[0] */
1167c478bd9Sstevel@tonic-gate 0, /* pad[1] */
1177c478bd9Sstevel@tonic-gate { 0 }, /* uberflags */
1187c478bd9Sstevel@tonic-gate NULL, /* queue_head */
1197c478bd9Sstevel@tonic-gate init_hash_table, /* thr_hash_table */
1207c478bd9Sstevel@tonic-gate 1, /* hash_size: size of the hash table */
1217c478bd9Sstevel@tonic-gate 0, /* hash_mask: hash_size - 1 */
1227c478bd9Sstevel@tonic-gate NULL, /* ulwp_one */
1237c478bd9Sstevel@tonic-gate NULL, /* all_lwps */
1247c478bd9Sstevel@tonic-gate NULL, /* all_zombies */
1257c478bd9Sstevel@tonic-gate 0, /* nthreads */
1267c478bd9Sstevel@tonic-gate 0, /* nzombies */
1277c478bd9Sstevel@tonic-gate 0, /* ndaemons */
1287c478bd9Sstevel@tonic-gate 0, /* pid */
1297c478bd9Sstevel@tonic-gate sigacthandler, /* sigacthandler */
1307c478bd9Sstevel@tonic-gate NULL, /* lwp_stacks */
1317c478bd9Sstevel@tonic-gate NULL, /* lwp_laststack */
1327c478bd9Sstevel@tonic-gate 0, /* nfreestack */
1337c478bd9Sstevel@tonic-gate 10, /* thread_stack_cache */
1347c478bd9Sstevel@tonic-gate NULL, /* ulwp_freelist */
1357c478bd9Sstevel@tonic-gate NULL, /* ulwp_lastfree */
1367c478bd9Sstevel@tonic-gate NULL, /* ulwp_replace_free */
1377c478bd9Sstevel@tonic-gate NULL, /* ulwp_replace_last */
1387c478bd9Sstevel@tonic-gate NULL, /* atforklist */
139883492d5Sraf NULL, /* robustlocks */
14009ce0d4aSRoger A. Faulkner NULL, /* robustlist */
14123a1cceaSRoger A. Faulkner NULL, /* progname */
1422428aad8SPatrick Mooney NULL, /* ub_comm_page */
1437c478bd9Sstevel@tonic-gate NULL, /* __tdb_bootstrap */
1447c478bd9Sstevel@tonic-gate { /* tdb */
1457c478bd9Sstevel@tonic-gate NULL, /* tdb_sync_addr_hash */
1467c478bd9Sstevel@tonic-gate 0, /* tdb_register_count */
1477c478bd9Sstevel@tonic-gate 0, /* tdb_hash_alloc_failed */
1487c478bd9Sstevel@tonic-gate NULL, /* tdb_sync_addr_free */
1497c478bd9Sstevel@tonic-gate NULL, /* tdb_sync_addr_last */
1507c478bd9Sstevel@tonic-gate 0, /* tdb_sync_alloc */
1517c478bd9Sstevel@tonic-gate { 0, 0 }, /* tdb_ev_global_mask */
1527c478bd9Sstevel@tonic-gate tdb_events, /* tdb_events array */
1537c478bd9Sstevel@tonic-gate },
1547c478bd9Sstevel@tonic-gate };
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate * The weak version is known to libc_db and mdb.
1587c478bd9Sstevel@tonic-gate */
1597c478bd9Sstevel@tonic-gate #pragma weak _tdb_bootstrap = __tdb_bootstrap
1607c478bd9Sstevel@tonic-gate uberdata_t **__tdb_bootstrap = NULL;
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate int thread_queue_fifo = 4;
1637c478bd9Sstevel@tonic-gate int thread_queue_dump = 0;
1647c478bd9Sstevel@tonic-gate int thread_cond_wait_defer = 0;
1657c478bd9Sstevel@tonic-gate int thread_error_detection = 0;
1667c478bd9Sstevel@tonic-gate int thread_async_safe = 0;
1677c478bd9Sstevel@tonic-gate int thread_stack_cache = 10;
1687c478bd9Sstevel@tonic-gate int thread_door_noreserve = 0;
1697c5714f6Sraf int thread_locks_misaligned = 0;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate static ulwp_t *ulwp_alloc(void);
1727c478bd9Sstevel@tonic-gate static void ulwp_free(ulwp_t *);
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate * Insert the lwp into the hash table.
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate void
hash_in_unlocked(ulwp_t * ulwp,int ix,uberdata_t * udp)1787c478bd9Sstevel@tonic-gate hash_in_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate ulwp->ul_hash = udp->thr_hash_table[ix].hash_bucket;
1817c478bd9Sstevel@tonic-gate udp->thr_hash_table[ix].hash_bucket = ulwp;
1827c478bd9Sstevel@tonic-gate ulwp->ul_ix = ix;
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate void
hash_in(ulwp_t * ulwp,uberdata_t * udp)1867c478bd9Sstevel@tonic-gate hash_in(ulwp_t *ulwp, uberdata_t *udp)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate int ix = TIDHASH(ulwp->ul_lwpid, udp);
1897c478bd9Sstevel@tonic-gate mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate lmutex_lock(mp);
1927c478bd9Sstevel@tonic-gate hash_in_unlocked(ulwp, ix, udp);
1937c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate * Delete the lwp from the hash table.
1987c478bd9Sstevel@tonic-gate */
1997c478bd9Sstevel@tonic-gate void
hash_out_unlocked(ulwp_t * ulwp,int ix,uberdata_t * udp)2007c478bd9Sstevel@tonic-gate hash_out_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
2057c478bd9Sstevel@tonic-gate ulwp != *ulwpp;
2067c478bd9Sstevel@tonic-gate ulwpp = &(*ulwpp)->ul_hash)
2077c478bd9Sstevel@tonic-gate ;
2087c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_hash;
2097c478bd9Sstevel@tonic-gate ulwp->ul_hash = NULL;
2107c478bd9Sstevel@tonic-gate ulwp->ul_ix = -1;
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate void
hash_out(ulwp_t * ulwp,uberdata_t * udp)2147c478bd9Sstevel@tonic-gate hash_out(ulwp_t *ulwp, uberdata_t *udp)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate int ix;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate if ((ix = ulwp->ul_ix) >= 0) {
2197c478bd9Sstevel@tonic-gate mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate lmutex_lock(mp);
2227c478bd9Sstevel@tonic-gate hash_out_unlocked(ulwp, ix, udp);
2237c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
227a574db85Sraf /*
228a574db85Sraf * Retain stack information for thread structures that are being recycled for
229a574db85Sraf * new threads. All other members of the thread structure should be zeroed.
230a574db85Sraf */
2317c478bd9Sstevel@tonic-gate static void
ulwp_clean(ulwp_t * ulwp)2327c478bd9Sstevel@tonic-gate ulwp_clean(ulwp_t *ulwp)
2337c478bd9Sstevel@tonic-gate {
234a574db85Sraf caddr_t stk = ulwp->ul_stk;
235a574db85Sraf size_t mapsiz = ulwp->ul_mapsiz;
236a574db85Sraf size_t guardsize = ulwp->ul_guardsize;
237a574db85Sraf uintptr_t stktop = ulwp->ul_stktop;
238a574db85Sraf size_t stksiz = ulwp->ul_stksiz;
239a574db85Sraf
2408cd45542Sraf (void) memset(ulwp, 0, sizeof (*ulwp));
241a574db85Sraf
242a574db85Sraf ulwp->ul_stk = stk;
243a574db85Sraf ulwp->ul_mapsiz = mapsiz;
244a574db85Sraf ulwp->ul_guardsize = guardsize;
245a574db85Sraf ulwp->ul_stktop = stktop;
246a574db85Sraf ulwp->ul_stksiz = stksiz;
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate static int stackprot;
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate * Answer the question, "Is the lwp in question really dead?"
2537c478bd9Sstevel@tonic-gate * We must inquire of the operating system to be really sure
2547c478bd9Sstevel@tonic-gate * because the lwp may have called lwp_exit() but it has not
2557c478bd9Sstevel@tonic-gate * yet completed the exit.
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate static int
dead_and_buried(ulwp_t * ulwp)2587c478bd9Sstevel@tonic-gate dead_and_buried(ulwp_t *ulwp)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate if (ulwp->ul_lwpid == (lwpid_t)(-1))
2617c478bd9Sstevel@tonic-gate return (1);
2627c478bd9Sstevel@tonic-gate if (ulwp->ul_dead && ulwp->ul_detached &&
2637257d1b4Sraf _lwp_kill(ulwp->ul_lwpid, 0) == ESRCH) {
2647c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1);
2657c478bd9Sstevel@tonic-gate return (1);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate return (0);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate * Attempt to keep the stack cache within the specified cache limit.
2727c478bd9Sstevel@tonic-gate */
2737c478bd9Sstevel@tonic-gate static void
trim_stack_cache(int cache_limit)2747c478bd9Sstevel@tonic-gate trim_stack_cache(int cache_limit)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
2777c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
2787c478bd9Sstevel@tonic-gate ulwp_t *prev = NULL;
2797c478bd9Sstevel@tonic-gate ulwp_t **ulwpp = &udp->lwp_stacks;
2807c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, self));
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate while (udp->nfreestack > cache_limit && (ulwp = *ulwpp) != NULL) {
2857c478bd9Sstevel@tonic-gate if (dead_and_buried(ulwp)) {
2867c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_next;
2877c478bd9Sstevel@tonic-gate if (ulwp == udp->lwp_laststack)
2887c478bd9Sstevel@tonic-gate udp->lwp_laststack = prev;
2897c478bd9Sstevel@tonic-gate hash_out(ulwp, udp);
2907c478bd9Sstevel@tonic-gate udp->nfreestack--;
2918cd45542Sraf (void) munmap(ulwp->ul_stk, ulwp->ul_mapsiz);
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate * Now put the free ulwp on the ulwp freelist.
2947c478bd9Sstevel@tonic-gate */
2957c478bd9Sstevel@tonic-gate ulwp->ul_mapsiz = 0;
2967c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
2977c478bd9Sstevel@tonic-gate if (udp->ulwp_freelist == NULL)
2987c478bd9Sstevel@tonic-gate udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
2997c478bd9Sstevel@tonic-gate else {
3007c478bd9Sstevel@tonic-gate udp->ulwp_lastfree->ul_next = ulwp;
3017c478bd9Sstevel@tonic-gate udp->ulwp_lastfree = ulwp;
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate } else {
3047c478bd9Sstevel@tonic-gate prev = ulwp;
3057c478bd9Sstevel@tonic-gate ulwpp = &ulwp->ul_next;
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate * Find an unused stack of the requested size
3127c478bd9Sstevel@tonic-gate * or create a new stack of the requested size.
3137c478bd9Sstevel@tonic-gate * Return a pointer to the ulwp_t structure referring to the stack, or NULL.
3147c478bd9Sstevel@tonic-gate * thr_exit() stores 1 in the ul_dead member.
3157c478bd9Sstevel@tonic-gate * thr_join() stores -1 in the ul_lwpid member.
3167c478bd9Sstevel@tonic-gate */
3173db34912SRoger A. Faulkner static ulwp_t *
find_stack(size_t stksize,size_t guardsize)3187c478bd9Sstevel@tonic-gate find_stack(size_t stksize, size_t guardsize)
3197c478bd9Sstevel@tonic-gate {
32034709573Sraf static size_t pagesize = 0;
32134709573Sraf
3227c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
3237c478bd9Sstevel@tonic-gate size_t mapsize;
3247c478bd9Sstevel@tonic-gate ulwp_t *prev;
3257c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
3267c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
3277c478bd9Sstevel@tonic-gate void *stk;
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate * The stack is allocated PROT_READ|PROT_WRITE|PROT_EXEC
3317c478bd9Sstevel@tonic-gate * unless overridden by the system's configuration.
3327c478bd9Sstevel@tonic-gate */
3337c478bd9Sstevel@tonic-gate if (stackprot == 0) { /* do this once */
3347c478bd9Sstevel@tonic-gate long lprot = _sysconf(_SC_STACK_PROT);
3357c478bd9Sstevel@tonic-gate if (lprot <= 0)
3367c478bd9Sstevel@tonic-gate lprot = (PROT_READ|PROT_WRITE|PROT_EXEC);
3377c478bd9Sstevel@tonic-gate stackprot = (int)lprot;
3387c478bd9Sstevel@tonic-gate }
33934709573Sraf if (pagesize == 0) /* do this once */
34034709573Sraf pagesize = _sysconf(_SC_PAGESIZE);
34134709573Sraf
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate * One megabyte stacks by default, but subtract off
3447c478bd9Sstevel@tonic-gate * two pages for the system-created red zones.
3457c478bd9Sstevel@tonic-gate * Round up a non-zero stack size to a pagesize multiple.
3467c478bd9Sstevel@tonic-gate */
3477c478bd9Sstevel@tonic-gate if (stksize == 0)
34834709573Sraf stksize = DEFAULTSTACK - 2 * pagesize;
3497c478bd9Sstevel@tonic-gate else
35034709573Sraf stksize = ((stksize + pagesize - 1) & -pagesize);
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate * Round up the mapping size to a multiple of pagesize.
3547c478bd9Sstevel@tonic-gate * Note: mmap() provides at least one page of red zone
3557c478bd9Sstevel@tonic-gate * so we deduct that from the value of guardsize.
3567c478bd9Sstevel@tonic-gate */
3577c478bd9Sstevel@tonic-gate if (guardsize != 0)
35834709573Sraf guardsize = ((guardsize + pagesize - 1) & -pagesize) - pagesize;
3597c478bd9Sstevel@tonic-gate mapsize = stksize + guardsize;
3607c478bd9Sstevel@tonic-gate
3617c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
3627c478bd9Sstevel@tonic-gate for (prev = NULL, ulwpp = &udp->lwp_stacks;
3637c478bd9Sstevel@tonic-gate (ulwp = *ulwpp) != NULL;
3647c478bd9Sstevel@tonic-gate prev = ulwp, ulwpp = &ulwp->ul_next) {
3657c478bd9Sstevel@tonic-gate if (ulwp->ul_mapsiz == mapsize &&
3667c478bd9Sstevel@tonic-gate ulwp->ul_guardsize == guardsize &&
3677c478bd9Sstevel@tonic-gate dead_and_buried(ulwp)) {
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate * The previous lwp is gone; reuse the stack.
3707c478bd9Sstevel@tonic-gate * Remove the ulwp from the stack list.
3717c478bd9Sstevel@tonic-gate */
3727c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_next;
3737c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
3747c478bd9Sstevel@tonic-gate if (ulwp == udp->lwp_laststack)
3757c478bd9Sstevel@tonic-gate udp->lwp_laststack = prev;
3767c478bd9Sstevel@tonic-gate hash_out(ulwp, udp);
3777c478bd9Sstevel@tonic-gate udp->nfreestack--;
3787c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
3797c478bd9Sstevel@tonic-gate ulwp_clean(ulwp);
3807c478bd9Sstevel@tonic-gate return (ulwp);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate * None of the cached stacks matched our mapping size.
3867c478bd9Sstevel@tonic-gate * Reduce the stack cache to get rid of possibly
3877c478bd9Sstevel@tonic-gate * very old stacks that will never be reused.
3887c478bd9Sstevel@tonic-gate */
3897c478bd9Sstevel@tonic-gate if (udp->nfreestack > udp->thread_stack_cache)
3907c478bd9Sstevel@tonic-gate trim_stack_cache(udp->thread_stack_cache);
3917c478bd9Sstevel@tonic-gate else if (udp->nfreestack > 0)
3927c478bd9Sstevel@tonic-gate trim_stack_cache(udp->nfreestack - 1);
3937c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate * Create a new stack.
3977c478bd9Sstevel@tonic-gate */
3988cd45542Sraf if ((stk = mmap(NULL, mapsize, stackprot,
3997c478bd9Sstevel@tonic-gate MAP_PRIVATE|MAP_NORESERVE|MAP_ANON, -1, (off_t)0)) != MAP_FAILED) {
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate * We have allocated our stack. Now allocate the ulwp.
4027c478bd9Sstevel@tonic-gate */
4037c478bd9Sstevel@tonic-gate ulwp = ulwp_alloc();
4047c478bd9Sstevel@tonic-gate if (ulwp == NULL)
4058cd45542Sraf (void) munmap(stk, mapsize);
4067c478bd9Sstevel@tonic-gate else {
4077c478bd9Sstevel@tonic-gate ulwp->ul_stk = stk;
4087c478bd9Sstevel@tonic-gate ulwp->ul_mapsiz = mapsize;
4097c478bd9Sstevel@tonic-gate ulwp->ul_guardsize = guardsize;
4107c478bd9Sstevel@tonic-gate ulwp->ul_stktop = (uintptr_t)stk + mapsize;
4117c478bd9Sstevel@tonic-gate ulwp->ul_stksiz = stksize;
4127c478bd9Sstevel@tonic-gate if (guardsize) /* protect the extra red zone */
4138cd45542Sraf (void) mprotect(stk, guardsize, PROT_NONE);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate return (ulwp);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate /*
4207c478bd9Sstevel@tonic-gate * Get a ulwp_t structure from the free list or allocate a new one.
4217c478bd9Sstevel@tonic-gate * Such ulwp_t's do not have a stack allocated by the library.
4227c478bd9Sstevel@tonic-gate */
4237c478bd9Sstevel@tonic-gate static ulwp_t *
ulwp_alloc(void)4247c478bd9Sstevel@tonic-gate ulwp_alloc(void)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
4277c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
4287c478bd9Sstevel@tonic-gate size_t tls_size;
4297c478bd9Sstevel@tonic-gate ulwp_t *prev;
4307c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
4317c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
4327c478bd9Sstevel@tonic-gate caddr_t data;
4337c478bd9Sstevel@tonic-gate
4347c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
4357c478bd9Sstevel@tonic-gate for (prev = NULL, ulwpp = &udp->ulwp_freelist;
4367c478bd9Sstevel@tonic-gate (ulwp = *ulwpp) != NULL;
4377c478bd9Sstevel@tonic-gate prev = ulwp, ulwpp = &ulwp->ul_next) {
4387c478bd9Sstevel@tonic-gate if (dead_and_buried(ulwp)) {
4397c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_next;
4407c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
4417c478bd9Sstevel@tonic-gate if (ulwp == udp->ulwp_lastfree)
4427c478bd9Sstevel@tonic-gate udp->ulwp_lastfree = prev;
4437c478bd9Sstevel@tonic-gate hash_out(ulwp, udp);
4447c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
4457c478bd9Sstevel@tonic-gate ulwp_clean(ulwp);
4467c478bd9Sstevel@tonic-gate return (ulwp);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
4527c478bd9Sstevel@tonic-gate data = lmalloc(sizeof (*ulwp) + tls_size);
4537c478bd9Sstevel@tonic-gate if (data != NULL) {
4547c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
4557c478bd9Sstevel@tonic-gate ulwp = (ulwp_t *)(data + tls_size);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate return (ulwp);
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate /*
4617c478bd9Sstevel@tonic-gate * Free a ulwp structure.
4627c478bd9Sstevel@tonic-gate * If there is an associated stack, put it on the stack list and
4637c478bd9Sstevel@tonic-gate * munmap() previously freed stacks up to the residual cache limit.
4647c478bd9Sstevel@tonic-gate * Else put it on the ulwp free list and never call lfree() on it.
4657c478bd9Sstevel@tonic-gate */
4667c478bd9Sstevel@tonic-gate static void
ulwp_free(ulwp_t * ulwp)4677c478bd9Sstevel@tonic-gate ulwp_free(ulwp_t *ulwp)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, curthread));
4727c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
4737c478bd9Sstevel@tonic-gate if (ulwp == udp->ulwp_one) /* don't reuse the primoridal stack */
4747c478bd9Sstevel@tonic-gate /*EMPTY*/;
4757c478bd9Sstevel@tonic-gate else if (ulwp->ul_mapsiz != 0) {
4767c478bd9Sstevel@tonic-gate if (udp->lwp_stacks == NULL)
4777c478bd9Sstevel@tonic-gate udp->lwp_stacks = udp->lwp_laststack = ulwp;
4787c478bd9Sstevel@tonic-gate else {
4797c478bd9Sstevel@tonic-gate udp->lwp_laststack->ul_next = ulwp;
4807c478bd9Sstevel@tonic-gate udp->lwp_laststack = ulwp;
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate if (++udp->nfreestack > udp->thread_stack_cache)
4837c478bd9Sstevel@tonic-gate trim_stack_cache(udp->thread_stack_cache);
4847c478bd9Sstevel@tonic-gate } else {
4857c478bd9Sstevel@tonic-gate if (udp->ulwp_freelist == NULL)
4867c478bd9Sstevel@tonic-gate udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
4877c478bd9Sstevel@tonic-gate else {
4887c478bd9Sstevel@tonic-gate udp->ulwp_lastfree->ul_next = ulwp;
4897c478bd9Sstevel@tonic-gate udp->ulwp_lastfree = ulwp;
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate
4947c478bd9Sstevel@tonic-gate /*
4957c478bd9Sstevel@tonic-gate * Find a named lwp and return a pointer to its hash list location.
4967c478bd9Sstevel@tonic-gate * On success, returns with the hash lock held.
4977c478bd9Sstevel@tonic-gate */
4987c478bd9Sstevel@tonic-gate ulwp_t **
find_lwpp(thread_t tid)4997c478bd9Sstevel@tonic-gate find_lwpp(thread_t tid)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
5027c478bd9Sstevel@tonic-gate int ix = TIDHASH(tid, udp);
5037c478bd9Sstevel@tonic-gate mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
5047c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
5057c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate if (tid == 0)
5087c478bd9Sstevel@tonic-gate return (NULL);
5097c478bd9Sstevel@tonic-gate
5107c478bd9Sstevel@tonic-gate lmutex_lock(mp);
5117c478bd9Sstevel@tonic-gate for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
5127c478bd9Sstevel@tonic-gate (ulwp = *ulwpp) != NULL;
5137c478bd9Sstevel@tonic-gate ulwpp = &ulwp->ul_hash) {
5147c478bd9Sstevel@tonic-gate if (ulwp->ul_lwpid == tid)
5157c478bd9Sstevel@tonic-gate return (ulwpp);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
5187c478bd9Sstevel@tonic-gate return (NULL);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate * Wake up all lwps waiting on this lwp for some reason.
5237c478bd9Sstevel@tonic-gate */
5247c478bd9Sstevel@tonic-gate void
ulwp_broadcast(ulwp_t * ulwp)5257c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp_t *ulwp)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
5287c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
5317257d1b4Sraf (void) cond_broadcast(ulwp_condvar(ulwp, udp));
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate * Find a named lwp and return a pointer to it.
5367c478bd9Sstevel@tonic-gate * Returns with the hash lock held.
5377c478bd9Sstevel@tonic-gate */
5387c478bd9Sstevel@tonic-gate ulwp_t *
find_lwp(thread_t tid)5397c478bd9Sstevel@tonic-gate find_lwp(thread_t tid)
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
5427c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
5437c478bd9Sstevel@tonic-gate ulwp_t *ulwp = NULL;
5447c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate if (self->ul_lwpid == tid) {
5477c478bd9Sstevel@tonic-gate ulwp = self;
5487c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp);
5497c478bd9Sstevel@tonic-gate } else if ((ulwpp = find_lwpp(tid)) != NULL) {
5507c478bd9Sstevel@tonic-gate ulwp = *ulwpp;
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate if (ulwp && ulwp->ul_dead) {
5547c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
5557c478bd9Sstevel@tonic-gate ulwp = NULL;
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate return (ulwp);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate int
_thrp_create(void * stk,size_t stksize,void * (* func)(void *),void * arg,long flags,thread_t * new_thread,size_t guardsize,const char * name)5627c478bd9Sstevel@tonic-gate _thrp_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
563ab618543SJohn Levon long flags, thread_t *new_thread, size_t guardsize, const char *name)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
5667c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
5677c478bd9Sstevel@tonic-gate ucontext_t uc;
5687c478bd9Sstevel@tonic-gate uint_t lwp_flags;
5697c478bd9Sstevel@tonic-gate thread_t tid;
570373d25a2SRoger A. Faulkner int error;
5717c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate /*
5747c478bd9Sstevel@tonic-gate * Enforce the restriction of not creating any threads
5757c478bd9Sstevel@tonic-gate * until the primary link map has been initialized.
5767c478bd9Sstevel@tonic-gate * Also, disallow thread creation to a child of vfork().
5777c478bd9Sstevel@tonic-gate */
5787c478bd9Sstevel@tonic-gate if (!self->ul_primarymap || self->ul_vfork)
5797c478bd9Sstevel@tonic-gate return (ENOTSUP);
5807c478bd9Sstevel@tonic-gate
5817c478bd9Sstevel@tonic-gate if (udp->hash_size == 1)
5827c478bd9Sstevel@tonic-gate finish_init();
5837c478bd9Sstevel@tonic-gate
584d4204c85Sraf if ((stk || stksize) && stksize < MINSTACK)
5857c478bd9Sstevel@tonic-gate return (EINVAL);
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate if (stk == NULL) {
5887c478bd9Sstevel@tonic-gate if ((ulwp = find_stack(stksize, guardsize)) == NULL)
5897c478bd9Sstevel@tonic-gate return (ENOMEM);
5907c478bd9Sstevel@tonic-gate stksize = ulwp->ul_mapsiz - ulwp->ul_guardsize;
5917c478bd9Sstevel@tonic-gate } else {
5927c478bd9Sstevel@tonic-gate /* initialize the private stack */
5937c478bd9Sstevel@tonic-gate if ((ulwp = ulwp_alloc()) == NULL)
5947c478bd9Sstevel@tonic-gate return (ENOMEM);
5957c478bd9Sstevel@tonic-gate ulwp->ul_stk = stk;
5967c478bd9Sstevel@tonic-gate ulwp->ul_stktop = (uintptr_t)stk + stksize;
5977c478bd9Sstevel@tonic-gate ulwp->ul_stksiz = stksize;
5987c478bd9Sstevel@tonic-gate }
5993db34912SRoger A. Faulkner /* ulwp is not in the hash table; make sure hash_out() doesn't fail */
6003db34912SRoger A. Faulkner ulwp->ul_ix = -1;
6017c478bd9Sstevel@tonic-gate ulwp->ul_errnop = &ulwp->ul_errno;
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate lwp_flags = LWP_SUSPENDED;
6047c478bd9Sstevel@tonic-gate if (flags & (THR_DETACHED|THR_DAEMON)) {
6057c478bd9Sstevel@tonic-gate flags |= THR_DETACHED;
6067c478bd9Sstevel@tonic-gate lwp_flags |= LWP_DETACHED;
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate if (flags & THR_DAEMON)
6097c478bd9Sstevel@tonic-gate lwp_flags |= LWP_DAEMON;
6107c478bd9Sstevel@tonic-gate
6117257d1b4Sraf /* creating a thread: enforce mt-correctness in mutex_lock() */
6127c478bd9Sstevel@tonic-gate self->ul_async_safe = 1;
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate /* per-thread copies of global variables, for speed */
6157c478bd9Sstevel@tonic-gate ulwp->ul_queue_fifo = self->ul_queue_fifo;
6167c478bd9Sstevel@tonic-gate ulwp->ul_cond_wait_defer = self->ul_cond_wait_defer;
6177c478bd9Sstevel@tonic-gate ulwp->ul_error_detection = self->ul_error_detection;
6187c478bd9Sstevel@tonic-gate ulwp->ul_async_safe = self->ul_async_safe;
6197c478bd9Sstevel@tonic-gate ulwp->ul_max_spinners = self->ul_max_spinners;
6207c478bd9Sstevel@tonic-gate ulwp->ul_adaptive_spin = self->ul_adaptive_spin;
6217c478bd9Sstevel@tonic-gate ulwp->ul_queue_spin = self->ul_queue_spin;
6227c478bd9Sstevel@tonic-gate ulwp->ul_door_noreserve = self->ul_door_noreserve;
6237c5714f6Sraf ulwp->ul_misaligned = self->ul_misaligned;
6247c478bd9Sstevel@tonic-gate
625d4204c85Sraf /* new thread inherits creating thread's scheduling parameters */
626d4204c85Sraf ulwp->ul_policy = self->ul_policy;
627d4204c85Sraf ulwp->ul_pri = (self->ul_epri? self->ul_epri : self->ul_pri);
628d4204c85Sraf ulwp->ul_cid = self->ul_cid;
629d4204c85Sraf ulwp->ul_rtclassid = self->ul_rtclassid;
630d4204c85Sraf
6317c478bd9Sstevel@tonic-gate ulwp->ul_primarymap = self->ul_primarymap;
6327c478bd9Sstevel@tonic-gate ulwp->ul_self = ulwp;
6337c478bd9Sstevel@tonic-gate ulwp->ul_uberdata = udp;
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate /* debugger support */
6367c478bd9Sstevel@tonic-gate ulwp->ul_usropts = flags;
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate #ifdef __sparc
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate * We cache several instructions in the thread structure for use
6417c478bd9Sstevel@tonic-gate * by the fasttrap DTrace provider. When changing this, read the
6427c478bd9Sstevel@tonic-gate * comment in fasttrap.h for the all the other places that must
6437c478bd9Sstevel@tonic-gate * be changed.
6447c478bd9Sstevel@tonic-gate */
6457c478bd9Sstevel@tonic-gate ulwp->ul_dsave = 0x9de04000; /* save %g1, %g0, %sp */
6467c478bd9Sstevel@tonic-gate ulwp->ul_drestore = 0x81e80000; /* restore %g0, %g0, %g0 */
6477c478bd9Sstevel@tonic-gate ulwp->ul_dftret = 0x91d0203a; /* ta 0x3a */
6487c478bd9Sstevel@tonic-gate ulwp->ul_dreturn = 0x81ca0000; /* return %o0 */
6497c478bd9Sstevel@tonic-gate #endif
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate ulwp->ul_startpc = func;
6527c478bd9Sstevel@tonic-gate ulwp->ul_startarg = arg;
6537c478bd9Sstevel@tonic-gate _fpinherit(ulwp);
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate * Defer signals on the new thread until its TLS constructors
6567257d1b4Sraf * have been called. _thrp_setup() will call sigon() after
6577c478bd9Sstevel@tonic-gate * it has called tls_setup().
6587c478bd9Sstevel@tonic-gate */
6597c478bd9Sstevel@tonic-gate ulwp->ul_sigdefer = 1;
6607c478bd9Sstevel@tonic-gate
661373d25a2SRoger A. Faulkner error = setup_context(&uc, _thrp_setup, ulwp,
662373d25a2SRoger A. Faulkner (caddr_t)ulwp->ul_stk + ulwp->ul_guardsize, stksize);
663373d25a2SRoger A. Faulkner if (error != 0 && stk != NULL) /* inaccessible stack */
664373d25a2SRoger A. Faulkner error = EFAULT;
6657c478bd9Sstevel@tonic-gate
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate * Call enter_critical() to avoid being suspended until we
6687c478bd9Sstevel@tonic-gate * have linked the new thread into the proper lists.
6697c478bd9Sstevel@tonic-gate * This is necessary because forkall() and fork1() must
6707c478bd9Sstevel@tonic-gate * suspend all threads and they must see a complete list.
6717c478bd9Sstevel@tonic-gate */
6727c478bd9Sstevel@tonic-gate enter_critical(self);
6737c478bd9Sstevel@tonic-gate uc.uc_sigmask = ulwp->ul_sigmask = self->ul_sigmask;
6747c478bd9Sstevel@tonic-gate if (error != 0 ||
6757c478bd9Sstevel@tonic-gate (error = __lwp_create(&uc, lwp_flags, &tid)) != 0) {
6767c478bd9Sstevel@tonic-gate exit_critical(self);
6777c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1);
6787c478bd9Sstevel@tonic-gate ulwp->ul_dead = 1;
6797c478bd9Sstevel@tonic-gate ulwp->ul_detached = 1;
6807c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
6817c478bd9Sstevel@tonic-gate ulwp_free(ulwp);
6827c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
6837c478bd9Sstevel@tonic-gate return (error);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate self->ul_nocancel = 0; /* cancellation is now possible */
6867c478bd9Sstevel@tonic-gate udp->uberflags.uf_mt = 1;
6877c478bd9Sstevel@tonic-gate if (new_thread)
6887c478bd9Sstevel@tonic-gate *new_thread = tid;
6897c478bd9Sstevel@tonic-gate if (flags & THR_DETACHED)
6907c478bd9Sstevel@tonic-gate ulwp->ul_detached = 1;
6917c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = tid;
6927c478bd9Sstevel@tonic-gate ulwp->ul_stop = TSTP_REGULAR;
69336319254Sraf if (flags & THR_SUSPENDED)
69436319254Sraf ulwp->ul_created = 1;
6957c478bd9Sstevel@tonic-gate
6967c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
6977c478bd9Sstevel@tonic-gate ulwp->ul_forw = udp->all_lwps;
6987c478bd9Sstevel@tonic-gate ulwp->ul_back = udp->all_lwps->ul_back;
6997c478bd9Sstevel@tonic-gate ulwp->ul_back->ul_forw = ulwp;
7007c478bd9Sstevel@tonic-gate ulwp->ul_forw->ul_back = ulwp;
7017c478bd9Sstevel@tonic-gate hash_in(ulwp, udp);
7027c478bd9Sstevel@tonic-gate udp->nthreads++;
7037c478bd9Sstevel@tonic-gate if (flags & THR_DAEMON)
7047c478bd9Sstevel@tonic-gate udp->ndaemons++;
7057c478bd9Sstevel@tonic-gate if (flags & THR_NEW_LWP)
7067c478bd9Sstevel@tonic-gate thr_concurrency++;
7071d2738a5Sraf __libc_threaded = 1; /* inform stdio */
7087c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
7097c478bd9Sstevel@tonic-gate
7107c478bd9Sstevel@tonic-gate if (__td_event_report(self, TD_CREATE, udp)) {
7117c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_CREATE;
7127c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventdata = (void *)(uintptr_t)tid;
7137c478bd9Sstevel@tonic-gate tdb_event(TD_CREATE, udp);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate exit_critical(self);
71736319254Sraf
718*059afd40SRobert Mustacchi if (name != NULL && name[0] != '\0')
719ab618543SJohn Levon (void) pthread_setname_np(tid, name);
720ab618543SJohn Levon
72136319254Sraf if (!(flags & THR_SUSPENDED))
72236319254Sraf (void) _thrp_continue(tid, TSTP_REGULAR);
72336319254Sraf
7247c478bd9Sstevel@tonic-gate return (0);
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate int
thr_create(void * stk,size_t stksize,void * (* func)(void *),void * arg,long flags,thread_t * new_thread)7287257d1b4Sraf thr_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
729fc2512cfSRobert Mustacchi long flags, thread_t *new_thread)
7307c478bd9Sstevel@tonic-gate {
731ab618543SJohn Levon return (_thrp_create(stk, stksize, func, arg, flags, new_thread, 0,
732ab618543SJohn Levon NULL));
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate
7357c478bd9Sstevel@tonic-gate /*
7367c478bd9Sstevel@tonic-gate * A special cancellation cleanup hook for DCE.
7377c478bd9Sstevel@tonic-gate * cleanuphndlr, when it is not NULL, will contain a callback
7387c478bd9Sstevel@tonic-gate * function to be called before a thread is terminated in
7397257d1b4Sraf * thr_exit() as a result of being cancelled.
7407c478bd9Sstevel@tonic-gate */
7417c478bd9Sstevel@tonic-gate static void (*cleanuphndlr)(void) = NULL;
7427c478bd9Sstevel@tonic-gate
7437c478bd9Sstevel@tonic-gate /*
7447c478bd9Sstevel@tonic-gate * _pthread_setcleanupinit: sets the cleanup hook.
7457c478bd9Sstevel@tonic-gate */
7467c478bd9Sstevel@tonic-gate int
_pthread_setcleanupinit(void (* func)(void))7477c478bd9Sstevel@tonic-gate _pthread_setcleanupinit(void (*func)(void))
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate cleanuphndlr = func;
7507c478bd9Sstevel@tonic-gate return (0);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate void
_thrp_exit()7547c478bd9Sstevel@tonic-gate _thrp_exit()
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
7577c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
7587c478bd9Sstevel@tonic-gate ulwp_t *replace = NULL;
7597c478bd9Sstevel@tonic-gate
7607c478bd9Sstevel@tonic-gate if (__td_event_report(self, TD_DEATH, udp)) {
7617c478bd9Sstevel@tonic-gate self->ul_td_evbuf.eventnum = TD_DEATH;
7627c478bd9Sstevel@tonic-gate tdb_event(TD_DEATH, udp);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate
7657c478bd9Sstevel@tonic-gate ASSERT(self->ul_sigdefer != 0);
7667c478bd9Sstevel@tonic-gate
7677c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
7687c478bd9Sstevel@tonic-gate udp->nthreads--;
7697c478bd9Sstevel@tonic-gate if (self->ul_usropts & THR_NEW_LWP)
7707c478bd9Sstevel@tonic-gate thr_concurrency--;
7717c478bd9Sstevel@tonic-gate if (self->ul_usropts & THR_DAEMON)
7727c478bd9Sstevel@tonic-gate udp->ndaemons--;
7737c478bd9Sstevel@tonic-gate else if (udp->nthreads == udp->ndaemons) {
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate * We are the last non-daemon thread exiting.
7767c478bd9Sstevel@tonic-gate * Exit the process. We retain our TSD and TLS so
7777c478bd9Sstevel@tonic-gate * that atexit() application functions can use them.
7787c478bd9Sstevel@tonic-gate */
7797c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
7807c478bd9Sstevel@tonic-gate exit(0);
7817c478bd9Sstevel@tonic-gate thr_panic("_thrp_exit(): exit(0) returned");
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
7847c478bd9Sstevel@tonic-gate
7857c4ab494SYouzhong Yang /*
7867c4ab494SYouzhong Yang * tsd_exit() may call its destructor free(), thus depending on
7877c4ab494SYouzhong Yang * tmem, therefore tmem_exit() needs to be called after tsd_exit()
7887c4ab494SYouzhong Yang * and tls_exit().
7897c4ab494SYouzhong Yang */
790883492d5Sraf tsd_exit(); /* deallocate thread-specific data */
791883492d5Sraf tls_exit(); /* deallocate thread-local storage */
7927c4ab494SYouzhong Yang tmem_exit(); /* deallocate tmem allocations */
793883492d5Sraf heldlock_exit(); /* deal with left-over held locks */
7947c478bd9Sstevel@tonic-gate
7957c478bd9Sstevel@tonic-gate /* block all signals to finish exiting */
7967c478bd9Sstevel@tonic-gate block_all_signals(self);
7977c478bd9Sstevel@tonic-gate /* also prevent ourself from being suspended */
7987c478bd9Sstevel@tonic-gate enter_critical(self);
7997c478bd9Sstevel@tonic-gate rwl_free(self);
8007c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
8017c478bd9Sstevel@tonic-gate ulwp_free(self);
8027c478bd9Sstevel@tonic-gate (void) ulwp_lock(self, udp);
8037c478bd9Sstevel@tonic-gate
8047c478bd9Sstevel@tonic-gate if (self->ul_mapsiz && !self->ul_detached) {
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate * We want to free the stack for reuse but must keep
8077c478bd9Sstevel@tonic-gate * the ulwp_t struct for the benefit of thr_join().
8087c478bd9Sstevel@tonic-gate * For this purpose we allocate a replacement ulwp_t.
8097c478bd9Sstevel@tonic-gate */
8107c478bd9Sstevel@tonic-gate if ((replace = udp->ulwp_replace_free) == NULL)
8117c478bd9Sstevel@tonic-gate replace = lmalloc(REPLACEMENT_SIZE);
8127c478bd9Sstevel@tonic-gate else if ((udp->ulwp_replace_free = replace->ul_next) == NULL)
8137c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = NULL;
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate
8167c478bd9Sstevel@tonic-gate if (udp->all_lwps == self)
8177c478bd9Sstevel@tonic-gate udp->all_lwps = self->ul_forw;
8187c478bd9Sstevel@tonic-gate if (udp->all_lwps == self)
8197c478bd9Sstevel@tonic-gate udp->all_lwps = NULL;
8207c478bd9Sstevel@tonic-gate else {
8217c478bd9Sstevel@tonic-gate self->ul_forw->ul_back = self->ul_back;
8227c478bd9Sstevel@tonic-gate self->ul_back->ul_forw = self->ul_forw;
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate self->ul_forw = self->ul_back = NULL;
825d4204c85Sraf #if defined(THREAD_DEBUG)
8267c478bd9Sstevel@tonic-gate /* collect queue lock statistics before marking ourself dead */
8277c478bd9Sstevel@tonic-gate record_spin_locks(self);
828d4204c85Sraf #endif
8297c478bd9Sstevel@tonic-gate self->ul_dead = 1;
8307c478bd9Sstevel@tonic-gate self->ul_pleasestop = 0;
8317c478bd9Sstevel@tonic-gate if (replace != NULL) {
8327c478bd9Sstevel@tonic-gate int ix = self->ul_ix; /* the hash index */
8338cd45542Sraf (void) memcpy(replace, self, REPLACEMENT_SIZE);
8347c478bd9Sstevel@tonic-gate replace->ul_self = replace;
8357c478bd9Sstevel@tonic-gate replace->ul_next = NULL; /* clone not on stack list */
8367c478bd9Sstevel@tonic-gate replace->ul_mapsiz = 0; /* allows clone to be freed */
8377c478bd9Sstevel@tonic-gate replace->ul_replace = 1; /* requires clone to be freed */
8387c478bd9Sstevel@tonic-gate hash_out_unlocked(self, ix, udp);
8397c478bd9Sstevel@tonic-gate hash_in_unlocked(replace, ix, udp);
8407c478bd9Sstevel@tonic-gate ASSERT(!(self->ul_detached));
8417c478bd9Sstevel@tonic-gate self->ul_detached = 1; /* this frees the stack */
8427c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL;
8437c478bd9Sstevel@tonic-gate self->ul_schedctl_called = &udp->uberflags;
8447c478bd9Sstevel@tonic-gate set_curthread(self = replace);
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate * Having just changed the address of curthread, we
8477c478bd9Sstevel@tonic-gate * must reset the ownership of the locks we hold so
8487c478bd9Sstevel@tonic-gate * that assertions will not fire when we release them.
8497c478bd9Sstevel@tonic-gate */
8507c478bd9Sstevel@tonic-gate udp->link_lock.mutex_owner = (uintptr_t)self;
8517c478bd9Sstevel@tonic-gate ulwp_mutex(self, udp)->mutex_owner = (uintptr_t)self;
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate * NOTE:
8547c478bd9Sstevel@tonic-gate * On i386, %gs still references the original, not the
8557c478bd9Sstevel@tonic-gate * replacement, ulwp structure. Fetching the replacement
8567c478bd9Sstevel@tonic-gate * curthread pointer via %gs:0 works correctly since the
8577c478bd9Sstevel@tonic-gate * original ulwp structure will not be reallocated until
8587c478bd9Sstevel@tonic-gate * this lwp has completed its lwp_exit() system call (see
8597c478bd9Sstevel@tonic-gate * dead_and_buried()), but from here on out, we must make
8607c478bd9Sstevel@tonic-gate * no references to %gs:<offset> other than %gs:0.
8617c478bd9Sstevel@tonic-gate */
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate /*
8647c478bd9Sstevel@tonic-gate * Put non-detached terminated threads in the all_zombies list.
8657c478bd9Sstevel@tonic-gate */
8667c478bd9Sstevel@tonic-gate if (!self->ul_detached) {
8677c478bd9Sstevel@tonic-gate udp->nzombies++;
8687c478bd9Sstevel@tonic-gate if (udp->all_zombies == NULL) {
8697c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies == 1);
8707c478bd9Sstevel@tonic-gate udp->all_zombies = self->ul_forw = self->ul_back = self;
8717c478bd9Sstevel@tonic-gate } else {
8727c478bd9Sstevel@tonic-gate self->ul_forw = udp->all_zombies;
8737c478bd9Sstevel@tonic-gate self->ul_back = udp->all_zombies->ul_back;
8747c478bd9Sstevel@tonic-gate self->ul_back->ul_forw = self;
8757c478bd9Sstevel@tonic-gate self->ul_forw->ul_back = self;
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate * Notify everyone waiting for this thread.
8807c478bd9Sstevel@tonic-gate */
8817c478bd9Sstevel@tonic-gate ulwp_broadcast(self);
8827c478bd9Sstevel@tonic-gate (void) ulwp_unlock(self, udp);
8837c478bd9Sstevel@tonic-gate /*
8847c478bd9Sstevel@tonic-gate * Prevent any more references to the schedctl data.
8857c478bd9Sstevel@tonic-gate * We are exiting and continue_fork() may not find us.
8867c478bd9Sstevel@tonic-gate * Do this just before dropping link_lock, since fork
8877c478bd9Sstevel@tonic-gate * serializes on link_lock.
8887c478bd9Sstevel@tonic-gate */
8897c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL;
8907c478bd9Sstevel@tonic-gate self->ul_schedctl_called = &udp->uberflags;
8917c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
8927c478bd9Sstevel@tonic-gate
8937c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 1);
8947c478bd9Sstevel@tonic-gate ASSERT(self->ul_preempt == 0);
8957c478bd9Sstevel@tonic-gate _lwp_terminate(); /* never returns */
8967c478bd9Sstevel@tonic-gate thr_panic("_thrp_exit(): _lwp_terminate() returned");
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate
899d4204c85Sraf #if defined(THREAD_DEBUG)
9007c478bd9Sstevel@tonic-gate void
collect_queue_statistics()9017c478bd9Sstevel@tonic-gate collect_queue_statistics()
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
9047c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate if (thread_queue_dump) {
9077c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
9087c478bd9Sstevel@tonic-gate if ((ulwp = udp->all_lwps) != NULL) {
9097c478bd9Sstevel@tonic-gate do {
9107c478bd9Sstevel@tonic-gate record_spin_locks(ulwp);
9117c478bd9Sstevel@tonic-gate } while ((ulwp = ulwp->ul_forw) != udp->all_lwps);
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate }
916d4204c85Sraf #endif
9177c478bd9Sstevel@tonic-gate
9187257d1b4Sraf static void __NORETURN
_thrp_exit_common(void * status,int unwind)9197257d1b4Sraf _thrp_exit_common(void *status, int unwind)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
9227c478bd9Sstevel@tonic-gate int cancelled = (self->ul_cancel_pending && status == PTHREAD_CANCELED);
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 0 && self->ul_preempt == 0);
9257c478bd9Sstevel@tonic-gate
9267c478bd9Sstevel@tonic-gate /*
9277c478bd9Sstevel@tonic-gate * Disable cancellation and call the special DCE cancellation
9287c478bd9Sstevel@tonic-gate * cleanup hook if it is enabled. Do nothing else before calling
9297c478bd9Sstevel@tonic-gate * the DCE cancellation cleanup hook; it may call longjmp() and
9307c478bd9Sstevel@tonic-gate * never return here.
9317c478bd9Sstevel@tonic-gate */
9327c478bd9Sstevel@tonic-gate self->ul_cancel_disabled = 1;
9337c478bd9Sstevel@tonic-gate self->ul_cancel_async = 0;
9347c478bd9Sstevel@tonic-gate self->ul_save_async = 0;
9357c478bd9Sstevel@tonic-gate self->ul_cancelable = 0;
9367c478bd9Sstevel@tonic-gate self->ul_cancel_pending = 0;
937a574db85Sraf set_cancel_pending_flag(self, 1);
9387c478bd9Sstevel@tonic-gate if (cancelled && cleanuphndlr != NULL)
9397c478bd9Sstevel@tonic-gate (*cleanuphndlr)();
9407c478bd9Sstevel@tonic-gate
9417c478bd9Sstevel@tonic-gate /*
9427c478bd9Sstevel@tonic-gate * Block application signals while we are exiting.
9437c478bd9Sstevel@tonic-gate * We call out to C++, TSD, and TLS destructors while exiting
9447c478bd9Sstevel@tonic-gate * and these are application-defined, so we cannot be assured
9457c478bd9Sstevel@tonic-gate * that they won't reset the signal mask. We use sigoff() to
9467c478bd9Sstevel@tonic-gate * defer any signals that may be received as a result of this
9477c478bd9Sstevel@tonic-gate * bad behavior. Such signals will be lost to the process
9487c478bd9Sstevel@tonic-gate * when the thread finishes exiting.
9497c478bd9Sstevel@tonic-gate */
9507257d1b4Sraf (void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
9517c478bd9Sstevel@tonic-gate sigoff(self);
9527c478bd9Sstevel@tonic-gate
9537c478bd9Sstevel@tonic-gate self->ul_rval = status;
9547c478bd9Sstevel@tonic-gate
9557c478bd9Sstevel@tonic-gate /*
9567c478bd9Sstevel@tonic-gate * If thr_exit is being called from the places where
9577c478bd9Sstevel@tonic-gate * C++ destructors are to be called such as cancellation
9587c478bd9Sstevel@tonic-gate * points, then set this flag. It is checked in _t_cancel()
9597c478bd9Sstevel@tonic-gate * to decide whether _ex_unwind() is to be called or not.
9607c478bd9Sstevel@tonic-gate */
9617c478bd9Sstevel@tonic-gate if (unwind)
9627c478bd9Sstevel@tonic-gate self->ul_unwind = 1;
9637c478bd9Sstevel@tonic-gate
9647c478bd9Sstevel@tonic-gate /*
9657c478bd9Sstevel@tonic-gate * _thrp_unwind() will eventually call _thrp_exit().
9667c478bd9Sstevel@tonic-gate * It never returns.
9677c478bd9Sstevel@tonic-gate */
9687c478bd9Sstevel@tonic-gate _thrp_unwind(NULL);
9697257d1b4Sraf thr_panic("_thrp_exit_common(): _thrp_unwind() returned");
9707257d1b4Sraf
9717257d1b4Sraf for (;;) /* to shut the compiler up about __NORETURN */
9727257d1b4Sraf continue;
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate
9757c478bd9Sstevel@tonic-gate /*
9767c478bd9Sstevel@tonic-gate * Called when a thread returns from its start function.
9777c478bd9Sstevel@tonic-gate * We are at the top of the stack; no unwinding is necessary.
9787c478bd9Sstevel@tonic-gate */
9797c478bd9Sstevel@tonic-gate void
_thrp_terminate(void * status)9807257d1b4Sraf _thrp_terminate(void *status)
9817c478bd9Sstevel@tonic-gate {
9827257d1b4Sraf _thrp_exit_common(status, 0);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate
9857257d1b4Sraf #pragma weak pthread_exit = thr_exit
9867257d1b4Sraf #pragma weak _thr_exit = thr_exit
9877c478bd9Sstevel@tonic-gate void
thr_exit(void * status)9887257d1b4Sraf thr_exit(void *status)
9897c478bd9Sstevel@tonic-gate {
9907257d1b4Sraf _thrp_exit_common(status, 1);
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate
9937c478bd9Sstevel@tonic-gate int
_thrp_join(thread_t tid,thread_t * departed,void ** status,int do_cancel)9947c478bd9Sstevel@tonic-gate _thrp_join(thread_t tid, thread_t *departed, void **status, int do_cancel)
9957c478bd9Sstevel@tonic-gate {
9967c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
9977c478bd9Sstevel@tonic-gate mutex_t *mp;
9987c478bd9Sstevel@tonic-gate void *rval;
9997c478bd9Sstevel@tonic-gate thread_t found;
10007c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
10017c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
10027c478bd9Sstevel@tonic-gate int replace;
10037c478bd9Sstevel@tonic-gate int error;
10047c478bd9Sstevel@tonic-gate
10057c478bd9Sstevel@tonic-gate if (do_cancel)
10067c478bd9Sstevel@tonic-gate error = lwp_wait(tid, &found);
10077c478bd9Sstevel@tonic-gate else {
10087c478bd9Sstevel@tonic-gate while ((error = __lwp_wait(tid, &found)) == EINTR)
10097c478bd9Sstevel@tonic-gate ;
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate if (error)
10127c478bd9Sstevel@tonic-gate return (error);
10137c478bd9Sstevel@tonic-gate
10147c478bd9Sstevel@tonic-gate /*
10157c478bd9Sstevel@tonic-gate * We must hold link_lock to avoid a race condition with find_stack().
10167c478bd9Sstevel@tonic-gate */
10177c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
10187c478bd9Sstevel@tonic-gate if ((ulwpp = find_lwpp(found)) == NULL) {
10197c478bd9Sstevel@tonic-gate /*
10207c478bd9Sstevel@tonic-gate * lwp_wait() found an lwp that the library doesn't know
10217c478bd9Sstevel@tonic-gate * about. It must have been created with _lwp_create().
10227c478bd9Sstevel@tonic-gate * Just return its lwpid; we can't know its status.
10237c478bd9Sstevel@tonic-gate */
10247c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
10257c478bd9Sstevel@tonic-gate rval = NULL;
10267c478bd9Sstevel@tonic-gate } else {
10277c478bd9Sstevel@tonic-gate /*
10287c478bd9Sstevel@tonic-gate * Remove ulwp from the hash table.
10297c478bd9Sstevel@tonic-gate */
10307c478bd9Sstevel@tonic-gate ulwp = *ulwpp;
10317c478bd9Sstevel@tonic-gate *ulwpp = ulwp->ul_hash;
10327c478bd9Sstevel@tonic-gate ulwp->ul_hash = NULL;
10337c478bd9Sstevel@tonic-gate /*
10347c478bd9Sstevel@tonic-gate * Remove ulwp from all_zombies list.
10357c478bd9Sstevel@tonic-gate */
10367c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies >= 1);
10377c478bd9Sstevel@tonic-gate if (udp->all_zombies == ulwp)
10387c478bd9Sstevel@tonic-gate udp->all_zombies = ulwp->ul_forw;
10397c478bd9Sstevel@tonic-gate if (udp->all_zombies == ulwp)
10407c478bd9Sstevel@tonic-gate udp->all_zombies = NULL;
10417c478bd9Sstevel@tonic-gate else {
10427c478bd9Sstevel@tonic-gate ulwp->ul_forw->ul_back = ulwp->ul_back;
10437c478bd9Sstevel@tonic-gate ulwp->ul_back->ul_forw = ulwp->ul_forw;
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate ulwp->ul_forw = ulwp->ul_back = NULL;
10467c478bd9Sstevel@tonic-gate udp->nzombies--;
10477c478bd9Sstevel@tonic-gate ASSERT(ulwp->ul_dead && !ulwp->ul_detached &&
10482e145884Sraf !(ulwp->ul_usropts & (THR_DETACHED|THR_DAEMON)));
10497c478bd9Sstevel@tonic-gate /*
10507c478bd9Sstevel@tonic-gate * We can't call ulwp_unlock(ulwp) after we set
10517c478bd9Sstevel@tonic-gate * ulwp->ul_ix = -1 so we have to get a pointer to the
10527c478bd9Sstevel@tonic-gate * ulwp's hash table mutex now in order to unlock it below.
10537c478bd9Sstevel@tonic-gate */
10547c478bd9Sstevel@tonic-gate mp = ulwp_mutex(ulwp, udp);
10557c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1);
10567c478bd9Sstevel@tonic-gate ulwp->ul_ix = -1;
10577c478bd9Sstevel@tonic-gate rval = ulwp->ul_rval;
10587c478bd9Sstevel@tonic-gate replace = ulwp->ul_replace;
10597c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
10607c478bd9Sstevel@tonic-gate if (replace) {
10617c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
10627c478bd9Sstevel@tonic-gate if (udp->ulwp_replace_free == NULL)
10637c478bd9Sstevel@tonic-gate udp->ulwp_replace_free =
10642e145884Sraf udp->ulwp_replace_last = ulwp;
10657c478bd9Sstevel@tonic-gate else {
10667c478bd9Sstevel@tonic-gate udp->ulwp_replace_last->ul_next = ulwp;
10677c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = ulwp;
10687c478bd9Sstevel@tonic-gate }
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate
10737c478bd9Sstevel@tonic-gate if (departed != NULL)
10747c478bd9Sstevel@tonic-gate *departed = found;
10757c478bd9Sstevel@tonic-gate if (status != NULL)
10767c478bd9Sstevel@tonic-gate *status = rval;
10777c478bd9Sstevel@tonic-gate return (0);
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate
10807c478bd9Sstevel@tonic-gate int
thr_join(thread_t tid,thread_t * departed,void ** status)10817257d1b4Sraf thr_join(thread_t tid, thread_t *departed, void **status)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate int error = _thrp_join(tid, departed, status, 1);
10847c478bd9Sstevel@tonic-gate return ((error == EINVAL)? ESRCH : error);
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate
10877c478bd9Sstevel@tonic-gate /*
10887c478bd9Sstevel@tonic-gate * pthread_join() differs from Solaris thr_join():
10897c478bd9Sstevel@tonic-gate * It does not return the departed thread's id
10907c478bd9Sstevel@tonic-gate * and hence does not have a "departed" argument.
10917c478bd9Sstevel@tonic-gate * It returns EINVAL if tid refers to a detached thread.
10927c478bd9Sstevel@tonic-gate */
10937257d1b4Sraf #pragma weak _pthread_join = pthread_join
10947c478bd9Sstevel@tonic-gate int
pthread_join(pthread_t tid,void ** status)10957257d1b4Sraf pthread_join(pthread_t tid, void **status)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate return ((tid == 0)? ESRCH : _thrp_join(tid, NULL, status, 1));
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate
11007c478bd9Sstevel@tonic-gate int
pthread_detach(pthread_t tid)11017257d1b4Sraf pthread_detach(pthread_t tid)
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
11047c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
11057c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
11067c478bd9Sstevel@tonic-gate int error = 0;
11077c478bd9Sstevel@tonic-gate
11087c478bd9Sstevel@tonic-gate if ((ulwpp = find_lwpp(tid)) == NULL)
11097c478bd9Sstevel@tonic-gate return (ESRCH);
11107c478bd9Sstevel@tonic-gate ulwp = *ulwpp;
11117c478bd9Sstevel@tonic-gate
11127c478bd9Sstevel@tonic-gate if (ulwp->ul_dead) {
11137c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
11147c478bd9Sstevel@tonic-gate error = _thrp_join(tid, NULL, NULL, 0);
11157c478bd9Sstevel@tonic-gate } else {
11167c478bd9Sstevel@tonic-gate error = __lwp_detach(tid);
11177c478bd9Sstevel@tonic-gate ulwp->ul_detached = 1;
11187c478bd9Sstevel@tonic-gate ulwp->ul_usropts |= THR_DETACHED;
11197c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate return (error);
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate
11247c478bd9Sstevel@tonic-gate static const char *
ematch(const char * ev,const char * match)11257c478bd9Sstevel@tonic-gate ematch(const char *ev, const char *match)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate int c;
11287c478bd9Sstevel@tonic-gate
11297c478bd9Sstevel@tonic-gate while ((c = *match++) != '\0') {
11307c478bd9Sstevel@tonic-gate if (*ev++ != c)
11317c478bd9Sstevel@tonic-gate return (NULL);
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate if (*ev++ != '=')
11347c478bd9Sstevel@tonic-gate return (NULL);
11357c478bd9Sstevel@tonic-gate return (ev);
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate
11387c478bd9Sstevel@tonic-gate static int
envvar(const char * ev,const char * match,int limit)11397c478bd9Sstevel@tonic-gate envvar(const char *ev, const char *match, int limit)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate int val = -1;
11427c478bd9Sstevel@tonic-gate const char *ename;
11437c478bd9Sstevel@tonic-gate
11447c478bd9Sstevel@tonic-gate if ((ename = ematch(ev, match)) != NULL) {
11457c478bd9Sstevel@tonic-gate int c;
11467c478bd9Sstevel@tonic-gate for (val = 0; (c = *ename) != '\0'; ename++) {
11477c478bd9Sstevel@tonic-gate if (!isdigit(c)) {
11487c478bd9Sstevel@tonic-gate val = -1;
11497c478bd9Sstevel@tonic-gate break;
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate val = val * 10 + (c - '0');
11527c478bd9Sstevel@tonic-gate if (val > limit) {
11537c478bd9Sstevel@tonic-gate val = limit;
11547c478bd9Sstevel@tonic-gate break;
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate return (val);
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate
11617c478bd9Sstevel@tonic-gate static void
etest(const char * ev)11627c478bd9Sstevel@tonic-gate etest(const char *ev)
11637c478bd9Sstevel@tonic-gate {
11647c478bd9Sstevel@tonic-gate int value;
11657c478bd9Sstevel@tonic-gate
11667c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_SPIN", 1000000)) >= 0)
11677c478bd9Sstevel@tonic-gate thread_queue_spin = value;
11685d1dd9a9Sraf if ((value = envvar(ev, "ADAPTIVE_SPIN", 1000000)) >= 0)
11697c478bd9Sstevel@tonic-gate thread_adaptive_spin = value;
11705d1dd9a9Sraf if ((value = envvar(ev, "MAX_SPINNERS", 255)) >= 0)
11717c478bd9Sstevel@tonic-gate thread_max_spinners = value;
11727c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_FIFO", 8)) >= 0)
11737c478bd9Sstevel@tonic-gate thread_queue_fifo = value;
11747c478bd9Sstevel@tonic-gate #if defined(THREAD_DEBUG)
11757c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_VERIFY", 1)) >= 0)
11767c478bd9Sstevel@tonic-gate thread_queue_verify = value;
11777c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "QUEUE_DUMP", 1)) >= 0)
11787c478bd9Sstevel@tonic-gate thread_queue_dump = value;
1179d4204c85Sraf #endif
11807c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "STACK_CACHE", 10000)) >= 0)
11817c478bd9Sstevel@tonic-gate thread_stack_cache = value;
11827c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "COND_WAIT_DEFER", 1)) >= 0)
11837c478bd9Sstevel@tonic-gate thread_cond_wait_defer = value;
11847c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "ERROR_DETECTION", 2)) >= 0)
11857c478bd9Sstevel@tonic-gate thread_error_detection = value;
11867c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "ASYNC_SAFE", 1)) >= 0)
11877c478bd9Sstevel@tonic-gate thread_async_safe = value;
11887c478bd9Sstevel@tonic-gate if ((value = envvar(ev, "DOOR_NORESERVE", 1)) >= 0)
11897c478bd9Sstevel@tonic-gate thread_door_noreserve = value;
11907c5714f6Sraf if ((value = envvar(ev, "LOCKS_MISALIGNED", 1)) >= 0)
11917c5714f6Sraf thread_locks_misaligned = value;
11927c478bd9Sstevel@tonic-gate }
11937c478bd9Sstevel@tonic-gate
11947c478bd9Sstevel@tonic-gate /*
11957c478bd9Sstevel@tonic-gate * Look for and evaluate environment variables of the form "_THREAD_*".
11967c478bd9Sstevel@tonic-gate * For compatibility with the past, we also look for environment
11977c478bd9Sstevel@tonic-gate * names of the form "LIBTHREAD_*".
11987c478bd9Sstevel@tonic-gate */
11997c478bd9Sstevel@tonic-gate static void
set_thread_vars()12007c478bd9Sstevel@tonic-gate set_thread_vars()
12017c478bd9Sstevel@tonic-gate {
120259f081edSraf extern const char **_environ;
12037c478bd9Sstevel@tonic-gate const char **pev;
12047c478bd9Sstevel@tonic-gate const char *ev;
12057c478bd9Sstevel@tonic-gate char c;
12067c478bd9Sstevel@tonic-gate
120759f081edSraf if ((pev = _environ) == NULL)
12087c478bd9Sstevel@tonic-gate return;
12097c478bd9Sstevel@tonic-gate while ((ev = *pev++) != NULL) {
12107c478bd9Sstevel@tonic-gate c = *ev;
12117257d1b4Sraf if (c == '_' && strncmp(ev, "_THREAD_", 8) == 0)
12127c478bd9Sstevel@tonic-gate etest(ev + 8);
12137257d1b4Sraf if (c == 'L' && strncmp(ev, "LIBTHREAD_", 10) == 0)
12147c478bd9Sstevel@tonic-gate etest(ev + 10);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate }
12177c478bd9Sstevel@tonic-gate
12187c478bd9Sstevel@tonic-gate /* same as atexit() but private to the library */
12197c478bd9Sstevel@tonic-gate extern int _atexit(void (*)(void));
12207c478bd9Sstevel@tonic-gate
12217c478bd9Sstevel@tonic-gate /* same as _cleanup() but private to the library */
12227c478bd9Sstevel@tonic-gate extern void __cleanup(void);
12237c478bd9Sstevel@tonic-gate
12247c478bd9Sstevel@tonic-gate extern void atfork_init(void);
12257c478bd9Sstevel@tonic-gate
12267c478bd9Sstevel@tonic-gate #ifdef __amd64
1227d0b3732eSbholler extern void __proc64id(void);
12287c478bd9Sstevel@tonic-gate #endif
12297c478bd9Sstevel@tonic-gate
12302428aad8SPatrick Mooney static void
init_auxv_data(uberdata_t * udp)12312428aad8SPatrick Mooney init_auxv_data(uberdata_t *udp)
12322428aad8SPatrick Mooney {
12332428aad8SPatrick Mooney Dl_argsinfo_t args;
12342428aad8SPatrick Mooney
12352428aad8SPatrick Mooney udp->ub_comm_page = NULL;
12362428aad8SPatrick Mooney if (dlinfo(RTLD_SELF, RTLD_DI_ARGSINFO, &args) < 0)
12372428aad8SPatrick Mooney return;
12382428aad8SPatrick Mooney
12392428aad8SPatrick Mooney while (args.dla_auxv->a_type != AT_NULL) {
12402428aad8SPatrick Mooney if (args.dla_auxv->a_type == AT_SUN_COMMPAGE) {
12412428aad8SPatrick Mooney udp->ub_comm_page = args.dla_auxv->a_un.a_ptr;
12422428aad8SPatrick Mooney }
12432428aad8SPatrick Mooney args.dla_auxv++;
12442428aad8SPatrick Mooney }
12452428aad8SPatrick Mooney }
12462428aad8SPatrick Mooney
12477c478bd9Sstevel@tonic-gate /*
12487c478bd9Sstevel@tonic-gate * libc_init() is called by ld.so.1 for library initialization.
12497c478bd9Sstevel@tonic-gate * We perform minimal initialization; enough to work with the main thread.
12507c478bd9Sstevel@tonic-gate */
12517c478bd9Sstevel@tonic-gate void
libc_init(void)12527c478bd9Sstevel@tonic-gate libc_init(void)
12537c478bd9Sstevel@tonic-gate {
12547c478bd9Sstevel@tonic-gate uberdata_t *udp = &__uberdata;
12557c478bd9Sstevel@tonic-gate ulwp_t *oldself = __curthread();
12567c478bd9Sstevel@tonic-gate ucontext_t uc;
12577c478bd9Sstevel@tonic-gate ulwp_t *self;
12587c478bd9Sstevel@tonic-gate struct rlimit rl;
12597c478bd9Sstevel@tonic-gate caddr_t data;
12607c478bd9Sstevel@tonic-gate size_t tls_size;
12617c478bd9Sstevel@tonic-gate int setmask;
12627c478bd9Sstevel@tonic-gate
12637c478bd9Sstevel@tonic-gate /*
12647c478bd9Sstevel@tonic-gate * For the initial stage of initialization, we must be careful
12657c478bd9Sstevel@tonic-gate * not to call any function that could possibly call _cerror().
12667c478bd9Sstevel@tonic-gate * For this purpose, we call only the raw system call wrappers.
12677c478bd9Sstevel@tonic-gate */
12687c478bd9Sstevel@tonic-gate
12697c478bd9Sstevel@tonic-gate #ifdef __amd64
12707c478bd9Sstevel@tonic-gate /*
12717c478bd9Sstevel@tonic-gate * Gather information about cache layouts for optimized
1272d0b3732eSbholler * AMD and Intel assembler strfoo() and memfoo() functions.
12737c478bd9Sstevel@tonic-gate */
1274d0b3732eSbholler __proc64id();
12757c478bd9Sstevel@tonic-gate #endif
12767c478bd9Sstevel@tonic-gate
12777c478bd9Sstevel@tonic-gate /*
12787c478bd9Sstevel@tonic-gate * Every libc, regardless of which link map, must register __cleanup().
12797c478bd9Sstevel@tonic-gate */
12807c478bd9Sstevel@tonic-gate (void) _atexit(__cleanup);
12817c478bd9Sstevel@tonic-gate
12822428aad8SPatrick Mooney /*
12832428aad8SPatrick Mooney * Every libc, regardless of link map, needs to go through and check
12842428aad8SPatrick Mooney * its aux vectors. Doing so will indicate whether or not this has
12852428aad8SPatrick Mooney * been given a comm page (to optimize certain system actions).
12862428aad8SPatrick Mooney */
12872428aad8SPatrick Mooney init_auxv_data(udp);
12882428aad8SPatrick Mooney
12897c478bd9Sstevel@tonic-gate /*
12907c478bd9Sstevel@tonic-gate * We keep our uberdata on one of (a) the first alternate link map
12917c478bd9Sstevel@tonic-gate * or (b) the primary link map. We switch to the primary link map
12927c478bd9Sstevel@tonic-gate * and stay there once we see it. All intermediate link maps are
12937c478bd9Sstevel@tonic-gate * subject to being unloaded at any time.
12947c478bd9Sstevel@tonic-gate */
12957c478bd9Sstevel@tonic-gate if (oldself != NULL && (oldself->ul_primarymap || !primary_link_map)) {
12967c478bd9Sstevel@tonic-gate __tdb_bootstrap = oldself->ul_uberdata->tdb_bootstrap;
12976a817834SRobert Mustacchi /*
12986a817834SRobert Mustacchi * Each link map has its own copy of the stack protector guard
12996a817834SRobert Mustacchi * and must always be initialized.
13006a817834SRobert Mustacchi */
13016a817834SRobert Mustacchi ssp_init();
13027c478bd9Sstevel@tonic-gate mutex_setup();
13037c478bd9Sstevel@tonic-gate atfork_init(); /* every link map needs atfork() processing */
130423a1cceaSRoger A. Faulkner init_progname();
13057c478bd9Sstevel@tonic-gate return;
13067c478bd9Sstevel@tonic-gate }
13077c478bd9Sstevel@tonic-gate
13087c478bd9Sstevel@tonic-gate /*
13097c478bd9Sstevel@tonic-gate * To establish the main stack information, we have to get our context.
13107c478bd9Sstevel@tonic-gate * This is also convenient to use for getting our signal mask.
13117c478bd9Sstevel@tonic-gate */
13127c478bd9Sstevel@tonic-gate uc.uc_flags = UC_ALL;
13138cd45542Sraf (void) __getcontext(&uc);
13147c478bd9Sstevel@tonic-gate ASSERT(uc.uc_link == NULL);
13157c478bd9Sstevel@tonic-gate
13167c478bd9Sstevel@tonic-gate tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
13177c478bd9Sstevel@tonic-gate ASSERT(primary_link_map || tls_size == 0);
13187c478bd9Sstevel@tonic-gate data = lmalloc(sizeof (ulwp_t) + tls_size);
13197c478bd9Sstevel@tonic-gate if (data == NULL)
13207c478bd9Sstevel@tonic-gate thr_panic("cannot allocate thread structure for main thread");
13217c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */
13227c478bd9Sstevel@tonic-gate self = (ulwp_t *)(data + tls_size);
13237c478bd9Sstevel@tonic-gate init_hash_table[0].hash_bucket = self;
13247c478bd9Sstevel@tonic-gate
13257c478bd9Sstevel@tonic-gate self->ul_sigmask = uc.uc_sigmask;
13267c478bd9Sstevel@tonic-gate delete_reserved_signals(&self->ul_sigmask);
13277c478bd9Sstevel@tonic-gate /*
13287c478bd9Sstevel@tonic-gate * Are the old and new sets different?
13297c478bd9Sstevel@tonic-gate * (This can happen if we are currently blocking SIGCANCEL.)
13307c478bd9Sstevel@tonic-gate * If so, we must explicitly set our signal mask, below.
13317c478bd9Sstevel@tonic-gate */
13327c478bd9Sstevel@tonic-gate setmask =
13337c478bd9Sstevel@tonic-gate ((self->ul_sigmask.__sigbits[0] ^ uc.uc_sigmask.__sigbits[0]) |
1334bdf0047cSRoger A. Faulkner (self->ul_sigmask.__sigbits[1] ^ uc.uc_sigmask.__sigbits[1]) |
1335bdf0047cSRoger A. Faulkner (self->ul_sigmask.__sigbits[2] ^ uc.uc_sigmask.__sigbits[2]) |
1336bdf0047cSRoger A. Faulkner (self->ul_sigmask.__sigbits[3] ^ uc.uc_sigmask.__sigbits[3]));
13377c478bd9Sstevel@tonic-gate
13387c478bd9Sstevel@tonic-gate #ifdef __sparc
13397c478bd9Sstevel@tonic-gate /*
13407c478bd9Sstevel@tonic-gate * We cache several instructions in the thread structure for use
13417c478bd9Sstevel@tonic-gate * by the fasttrap DTrace provider. When changing this, read the
13427c478bd9Sstevel@tonic-gate * comment in fasttrap.h for the all the other places that must
13437c478bd9Sstevel@tonic-gate * be changed.
13447c478bd9Sstevel@tonic-gate */
13457c478bd9Sstevel@tonic-gate self->ul_dsave = 0x9de04000; /* save %g1, %g0, %sp */
13467c478bd9Sstevel@tonic-gate self->ul_drestore = 0x81e80000; /* restore %g0, %g0, %g0 */
13477c478bd9Sstevel@tonic-gate self->ul_dftret = 0x91d0203a; /* ta 0x3a */
13487c478bd9Sstevel@tonic-gate self->ul_dreturn = 0x81ca0000; /* return %o0 */
13497c478bd9Sstevel@tonic-gate #endif
13507c478bd9Sstevel@tonic-gate
1351a574db85Sraf self->ul_stktop = (uintptr_t)uc.uc_stack.ss_sp + uc.uc_stack.ss_size;
13528cd45542Sraf (void) getrlimit(RLIMIT_STACK, &rl);
13537c478bd9Sstevel@tonic-gate self->ul_stksiz = rl.rlim_cur;
13547c478bd9Sstevel@tonic-gate self->ul_stk = (caddr_t)(self->ul_stktop - self->ul_stksiz);
13557c478bd9Sstevel@tonic-gate
13567c478bd9Sstevel@tonic-gate self->ul_forw = self->ul_back = self;
13577c478bd9Sstevel@tonic-gate self->ul_hash = NULL;
13587c478bd9Sstevel@tonic-gate self->ul_ix = 0;
13597257d1b4Sraf self->ul_lwpid = 1; /* _lwp_self() */
13607c478bd9Sstevel@tonic-gate self->ul_main = 1;
13617c478bd9Sstevel@tonic-gate self->ul_self = self;
1362d4204c85Sraf self->ul_policy = -1; /* initialize only when needed */
1363d4204c85Sraf self->ul_pri = 0;
1364d4204c85Sraf self->ul_cid = 0;
1365e84487aeSraf self->ul_rtclassid = -1;
13667c478bd9Sstevel@tonic-gate self->ul_uberdata = udp;
13677c478bd9Sstevel@tonic-gate if (oldself != NULL) {
13687c478bd9Sstevel@tonic-gate int i;
13697c478bd9Sstevel@tonic-gate
13707c478bd9Sstevel@tonic-gate ASSERT(primary_link_map);
13717c478bd9Sstevel@tonic-gate ASSERT(oldself->ul_main == 1);
13727c478bd9Sstevel@tonic-gate self->ul_stsd = oldself->ul_stsd;
13737c478bd9Sstevel@tonic-gate for (i = 0; i < TSD_NFAST; i++)
13747c478bd9Sstevel@tonic-gate self->ul_ftsd[i] = oldself->ul_ftsd[i];
13757c478bd9Sstevel@tonic-gate self->ul_tls = oldself->ul_tls;
13767c478bd9Sstevel@tonic-gate /*
13777c478bd9Sstevel@tonic-gate * Retrieve all pointers to uberdata allocated
13787c478bd9Sstevel@tonic-gate * while running on previous link maps.
13790293487cSraf * We would like to do a structure assignment here, but
13800293487cSraf * gcc turns structure assignments into calls to memcpy(),
13810293487cSraf * a function exported from libc. We can't call any such
13820293487cSraf * external functions until we establish curthread, below,
13830293487cSraf * so we just call our private version of memcpy().
13847c478bd9Sstevel@tonic-gate */
13858cd45542Sraf (void) memcpy(udp, oldself->ul_uberdata, sizeof (*udp));
13867c478bd9Sstevel@tonic-gate /*
13877c478bd9Sstevel@tonic-gate * These items point to global data on the primary link map.
13887c478bd9Sstevel@tonic-gate */
13897c478bd9Sstevel@tonic-gate udp->thr_hash_table = init_hash_table;
13907c478bd9Sstevel@tonic-gate udp->sigacthandler = sigacthandler;
13917c478bd9Sstevel@tonic-gate udp->tdb.tdb_events = tdb_events;
13927c478bd9Sstevel@tonic-gate ASSERT(udp->nthreads == 1 && !udp->uberflags.uf_mt);
13937c478bd9Sstevel@tonic-gate ASSERT(udp->lwp_stacks == NULL);
13947c478bd9Sstevel@tonic-gate ASSERT(udp->ulwp_freelist == NULL);
13957c478bd9Sstevel@tonic-gate ASSERT(udp->ulwp_replace_free == NULL);
13967c478bd9Sstevel@tonic-gate ASSERT(udp->hash_size == 1);
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate udp->all_lwps = self;
13997c478bd9Sstevel@tonic-gate udp->ulwp_one = self;
14008cd45542Sraf udp->pid = getpid();
14017c478bd9Sstevel@tonic-gate udp->nthreads = 1;
14027c478bd9Sstevel@tonic-gate /*
14037c478bd9Sstevel@tonic-gate * In every link map, tdb_bootstrap points to the same piece of
14047c478bd9Sstevel@tonic-gate * allocated memory. When the primary link map is initialized,
14057c478bd9Sstevel@tonic-gate * the allocated memory is assigned a pointer to the one true
14067c478bd9Sstevel@tonic-gate * uberdata. This allows libc_db to initialize itself regardless
14077c478bd9Sstevel@tonic-gate * of which instance of libc it finds in the address space.
14087c478bd9Sstevel@tonic-gate */
14097c478bd9Sstevel@tonic-gate if (udp->tdb_bootstrap == NULL)
14107c478bd9Sstevel@tonic-gate udp->tdb_bootstrap = lmalloc(sizeof (uberdata_t *));
14117c478bd9Sstevel@tonic-gate __tdb_bootstrap = udp->tdb_bootstrap;
14127c478bd9Sstevel@tonic-gate if (primary_link_map) {
14137c478bd9Sstevel@tonic-gate self->ul_primarymap = 1;
14147c478bd9Sstevel@tonic-gate udp->primary_map = 1;
14157c478bd9Sstevel@tonic-gate *udp->tdb_bootstrap = udp;
14167c478bd9Sstevel@tonic-gate }
14177c478bd9Sstevel@tonic-gate /*
14187c478bd9Sstevel@tonic-gate * Cancellation can't happen until:
14197c478bd9Sstevel@tonic-gate * pthread_cancel() is called
14207c478bd9Sstevel@tonic-gate * or:
14217c478bd9Sstevel@tonic-gate * another thread is created
14227c478bd9Sstevel@tonic-gate * For now, as a single-threaded process, set the flag that tells
14237c478bd9Sstevel@tonic-gate * PROLOGUE/EPILOGUE (in scalls.c) that cancellation can't happen.
14247c478bd9Sstevel@tonic-gate */
14257c478bd9Sstevel@tonic-gate self->ul_nocancel = 1;
14267c478bd9Sstevel@tonic-gate
14277c478bd9Sstevel@tonic-gate #if defined(__amd64)
1428ae115bc7Smrj (void) ___lwp_private(_LWP_SETPRIVATE, _LWP_FSBASE, self);
14297c478bd9Sstevel@tonic-gate #elif defined(__i386)
1430ae115bc7Smrj (void) ___lwp_private(_LWP_SETPRIVATE, _LWP_GSBASE, self);
14317c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */
14327c478bd9Sstevel@tonic-gate set_curthread(self); /* redundant on i386 */
14337c478bd9Sstevel@tonic-gate /*
14347c478bd9Sstevel@tonic-gate * Now curthread is established and it is safe to call any
14357c478bd9Sstevel@tonic-gate * function in libc except one that uses thread-local storage.
14367c478bd9Sstevel@tonic-gate */
14377c478bd9Sstevel@tonic-gate self->ul_errnop = &errno;
14387c478bd9Sstevel@tonic-gate if (oldself != NULL) {
14397c478bd9Sstevel@tonic-gate /* tls_size was zero when oldself was allocated */
14407c478bd9Sstevel@tonic-gate lfree(oldself, sizeof (ulwp_t));
14417c478bd9Sstevel@tonic-gate }
14426a817834SRobert Mustacchi ssp_init();
14437c478bd9Sstevel@tonic-gate mutex_setup();
14447c478bd9Sstevel@tonic-gate atfork_init();
14457c478bd9Sstevel@tonic-gate signal_init();
14467c478bd9Sstevel@tonic-gate
14477c478bd9Sstevel@tonic-gate /*
14487c478bd9Sstevel@tonic-gate * If the stack is unlimited, we set the size to zero to disable
14497c478bd9Sstevel@tonic-gate * stack checking.
14507c478bd9Sstevel@tonic-gate * XXX: Work harder here. Get the stack size from /proc/self/rmap
14517c478bd9Sstevel@tonic-gate */
14527c478bd9Sstevel@tonic-gate if (self->ul_stksiz == RLIM_INFINITY) {
14537c478bd9Sstevel@tonic-gate self->ul_ustack.ss_sp = (void *)self->ul_stktop;
14547c478bd9Sstevel@tonic-gate self->ul_ustack.ss_size = 0;
14557c478bd9Sstevel@tonic-gate } else {
14567c478bd9Sstevel@tonic-gate self->ul_ustack.ss_sp = self->ul_stk;
14577c478bd9Sstevel@tonic-gate self->ul_ustack.ss_size = self->ul_stksiz;
14587c478bd9Sstevel@tonic-gate }
14597c478bd9Sstevel@tonic-gate self->ul_ustack.ss_flags = 0;
14608cd45542Sraf (void) setustack(&self->ul_ustack);
14617c478bd9Sstevel@tonic-gate
14627c478bd9Sstevel@tonic-gate /*
14637c478bd9Sstevel@tonic-gate * Get the variables that affect thread behavior from the environment.
14647c478bd9Sstevel@tonic-gate */
14657c478bd9Sstevel@tonic-gate set_thread_vars();
14667c478bd9Sstevel@tonic-gate udp->uberflags.uf_thread_error_detection = (char)thread_error_detection;
14677c478bd9Sstevel@tonic-gate udp->thread_stack_cache = thread_stack_cache;
14687c478bd9Sstevel@tonic-gate
14697c478bd9Sstevel@tonic-gate /*
14707c478bd9Sstevel@tonic-gate * Make per-thread copies of global variables, for speed.
14717c478bd9Sstevel@tonic-gate */
14727c478bd9Sstevel@tonic-gate self->ul_queue_fifo = (char)thread_queue_fifo;
14737c478bd9Sstevel@tonic-gate self->ul_cond_wait_defer = (char)thread_cond_wait_defer;
14747c478bd9Sstevel@tonic-gate self->ul_error_detection = (char)thread_error_detection;
14757c478bd9Sstevel@tonic-gate self->ul_async_safe = (char)thread_async_safe;
14767c478bd9Sstevel@tonic-gate self->ul_door_noreserve = (char)thread_door_noreserve;
14777c5714f6Sraf self->ul_misaligned = (char)thread_locks_misaligned;
14785d1dd9a9Sraf self->ul_max_spinners = (uint8_t)thread_max_spinners;
14797c478bd9Sstevel@tonic-gate self->ul_adaptive_spin = thread_adaptive_spin;
14807c478bd9Sstevel@tonic-gate self->ul_queue_spin = thread_queue_spin;
14817c478bd9Sstevel@tonic-gate
148235e6f27aSRoger A. Faulkner #if defined(__sparc) && !defined(_LP64)
148335e6f27aSRoger A. Faulkner if (self->ul_misaligned) {
148435e6f27aSRoger A. Faulkner /*
148535e6f27aSRoger A. Faulkner * Tell the kernel to fix up ldx/stx instructions that
148635e6f27aSRoger A. Faulkner * refer to non-8-byte aligned data instead of giving
148735e6f27aSRoger A. Faulkner * the process an alignment trap and generating SIGBUS.
148835e6f27aSRoger A. Faulkner *
148935e6f27aSRoger A. Faulkner * Programs compiled for 32-bit sparc with the Studio SS12
149035e6f27aSRoger A. Faulkner * compiler get this done for them automatically (in _init()).
149135e6f27aSRoger A. Faulkner * We do it here for the benefit of programs compiled with
149235e6f27aSRoger A. Faulkner * other compilers, like gcc.
149335e6f27aSRoger A. Faulkner *
149435e6f27aSRoger A. Faulkner * This is necessary for the _THREAD_LOCKS_MISALIGNED=1
149535e6f27aSRoger A. Faulkner * environment variable horrible hack to work.
149635e6f27aSRoger A. Faulkner */
149735e6f27aSRoger A. Faulkner extern void _do_fix_align(void);
149835e6f27aSRoger A. Faulkner _do_fix_align();
149935e6f27aSRoger A. Faulkner }
150035e6f27aSRoger A. Faulkner #endif
150135e6f27aSRoger A. Faulkner
15027c478bd9Sstevel@tonic-gate /*
15037c478bd9Sstevel@tonic-gate * When we have initialized the primary link map, inform
15047c478bd9Sstevel@tonic-gate * the dynamic linker about our interface functions.
150523a1cceaSRoger A. Faulkner * Set up our pointer to the program name.
15067c478bd9Sstevel@tonic-gate */
15077c478bd9Sstevel@tonic-gate if (self->ul_primarymap)
15087c478bd9Sstevel@tonic-gate _ld_libc((void *)rtld_funcs);
150923a1cceaSRoger A. Faulkner init_progname();
15107c478bd9Sstevel@tonic-gate
15117c478bd9Sstevel@tonic-gate /*
15127c478bd9Sstevel@tonic-gate * Defer signals until TLS constructors have been called.
15137c478bd9Sstevel@tonic-gate */
15147c478bd9Sstevel@tonic-gate sigoff(self);
15157c478bd9Sstevel@tonic-gate tls_setup();
15167c478bd9Sstevel@tonic-gate sigon(self);
15177c478bd9Sstevel@tonic-gate if (setmask)
15187c478bd9Sstevel@tonic-gate (void) restore_signals(self);
15197c478bd9Sstevel@tonic-gate
1520a574db85Sraf /*
1521a574db85Sraf * Make private copies of __xpg4 and __xpg6 so libc can test
1522a574db85Sraf * them after this point without invoking the dynamic linker.
1523a574db85Sraf */
1524a574db85Sraf libc__xpg4 = __xpg4;
1525a574db85Sraf libc__xpg6 = __xpg6;
1526a574db85Sraf
1527f841f6adSraf init_sigev_thread();
1528f841f6adSraf init_aio();
152980598c2fSnakanon
153080598c2fSnakanon /*
153180598c2fSnakanon * We need to reset __threaded dynamically at runtime, so that
153280598c2fSnakanon * __threaded can be bound to __threaded outside libc which may not
153380598c2fSnakanon * have initial value of 1 (without a copy relocation in a.out).
153480598c2fSnakanon */
153580598c2fSnakanon __threaded = 1;
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate
15387c478bd9Sstevel@tonic-gate #pragma fini(libc_fini)
15397c478bd9Sstevel@tonic-gate void
libc_fini()15407c478bd9Sstevel@tonic-gate libc_fini()
15417c478bd9Sstevel@tonic-gate {
15427c478bd9Sstevel@tonic-gate /*
15437c478bd9Sstevel@tonic-gate * If we are doing fini processing for the instance of libc
15447c478bd9Sstevel@tonic-gate * on the first alternate link map (this happens only when
15457c478bd9Sstevel@tonic-gate * the dynamic linker rejects a bad audit library), then clear
15467c478bd9Sstevel@tonic-gate * __curthread(). We abandon whatever memory was allocated by
15477c478bd9Sstevel@tonic-gate * lmalloc() while running on this alternate link-map but we
15487c478bd9Sstevel@tonic-gate * don't care (and can't find the memory in any case); we just
15497c478bd9Sstevel@tonic-gate * want to protect the application from this bad audit library.
15507c478bd9Sstevel@tonic-gate * No fini processing is done by libc in the normal case.
15517c478bd9Sstevel@tonic-gate */
15527c478bd9Sstevel@tonic-gate
15537c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
15547c478bd9Sstevel@tonic-gate
15557c478bd9Sstevel@tonic-gate if (udp->primary_map == 0 && udp == &__uberdata)
15567c478bd9Sstevel@tonic-gate set_curthread(NULL);
15577c478bd9Sstevel@tonic-gate }
15587c478bd9Sstevel@tonic-gate
15597c478bd9Sstevel@tonic-gate /*
15607c478bd9Sstevel@tonic-gate * finish_init is called when we are about to become multi-threaded,
15617c478bd9Sstevel@tonic-gate * that is, on the first call to thr_create().
15627c478bd9Sstevel@tonic-gate */
15637c478bd9Sstevel@tonic-gate void
finish_init()15647c478bd9Sstevel@tonic-gate finish_init()
15657c478bd9Sstevel@tonic-gate {
15667c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
15677c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
15687c478bd9Sstevel@tonic-gate thr_hash_table_t *htp;
15697c478bd9Sstevel@tonic-gate void *data;
15707c478bd9Sstevel@tonic-gate int i;
15717c478bd9Sstevel@tonic-gate
15727c478bd9Sstevel@tonic-gate /*
15737c478bd9Sstevel@tonic-gate * No locks needed here; we are single-threaded on the first call.
15747c478bd9Sstevel@tonic-gate * We can be called only after the primary link map has been set up.
15757c478bd9Sstevel@tonic-gate */
15767c478bd9Sstevel@tonic-gate ASSERT(self->ul_primarymap);
15777c478bd9Sstevel@tonic-gate ASSERT(self == udp->ulwp_one);
15787c478bd9Sstevel@tonic-gate ASSERT(!udp->uberflags.uf_mt);
15797c478bd9Sstevel@tonic-gate ASSERT(udp->hash_size == 1);
15807c478bd9Sstevel@tonic-gate
15817c478bd9Sstevel@tonic-gate /*
1582d4204c85Sraf * Initialize self->ul_policy, self->ul_cid, and self->ul_pri.
1583d4204c85Sraf */
1584d4204c85Sraf update_sched(self);
1585d4204c85Sraf
1586d4204c85Sraf /*
1587d4204c85Sraf * Allocate the queue_head array if not already allocated.
15887c478bd9Sstevel@tonic-gate */
15897c478bd9Sstevel@tonic-gate if (udp->queue_head == NULL)
15907c478bd9Sstevel@tonic-gate queue_alloc();
15917c478bd9Sstevel@tonic-gate
15927c478bd9Sstevel@tonic-gate /*
15937c478bd9Sstevel@tonic-gate * Now allocate the thread hash table.
15947c478bd9Sstevel@tonic-gate */
15958cd45542Sraf if ((data = mmap(NULL, HASHTBLSZ * sizeof (thr_hash_table_t),
15967c478bd9Sstevel@tonic-gate PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, (off_t)0))
15977c478bd9Sstevel@tonic-gate == MAP_FAILED)
15987c478bd9Sstevel@tonic-gate thr_panic("cannot allocate thread hash table");
15997c478bd9Sstevel@tonic-gate
16007c478bd9Sstevel@tonic-gate udp->thr_hash_table = htp = (thr_hash_table_t *)data;
16017c478bd9Sstevel@tonic-gate udp->hash_size = HASHTBLSZ;
16027c478bd9Sstevel@tonic-gate udp->hash_mask = HASHTBLSZ - 1;
16037c478bd9Sstevel@tonic-gate
16047c478bd9Sstevel@tonic-gate for (i = 0; i < HASHTBLSZ; i++, htp++) {
1605883492d5Sraf htp->hash_lock.mutex_flag = LOCK_INITED;
16067c478bd9Sstevel@tonic-gate htp->hash_lock.mutex_magic = MUTEX_MAGIC;
16077c478bd9Sstevel@tonic-gate htp->hash_cond.cond_magic = COND_MAGIC;
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
16107c478bd9Sstevel@tonic-gate
16117c478bd9Sstevel@tonic-gate /*
16127c478bd9Sstevel@tonic-gate * Set up the SIGCANCEL handler for threads cancellation.
16137c478bd9Sstevel@tonic-gate */
1614f841f6adSraf setup_cancelsig(SIGCANCEL);
16157c478bd9Sstevel@tonic-gate
16167c478bd9Sstevel@tonic-gate /*
16177c478bd9Sstevel@tonic-gate * Arrange to do special things on exit --
16187c478bd9Sstevel@tonic-gate * - collect queue statistics from all remaining active threads.
1619d4204c85Sraf * - dump queue statistics to stderr if _THREAD_QUEUE_DUMP is set.
16207c478bd9Sstevel@tonic-gate * - grab assert_lock to ensure that assertion failures
16217c478bd9Sstevel@tonic-gate * and a core dump take precedence over _exit().
16227c478bd9Sstevel@tonic-gate * (Functions are called in the reverse order of their registration.)
16237c478bd9Sstevel@tonic-gate */
16247c478bd9Sstevel@tonic-gate (void) _atexit(grab_assert_lock);
1625d4204c85Sraf #if defined(THREAD_DEBUG)
1626d4204c85Sraf (void) _atexit(dump_queue_statistics);
16277c478bd9Sstevel@tonic-gate (void) _atexit(collect_queue_statistics);
1628d4204c85Sraf #endif
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate
16317c478bd9Sstevel@tonic-gate /*
1632657b1f3dSraf * Used only by postfork1_child(), below.
16337c478bd9Sstevel@tonic-gate */
16347c478bd9Sstevel@tonic-gate static void
mark_dead_and_buried(ulwp_t * ulwp)16357c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp_t *ulwp)
16367c478bd9Sstevel@tonic-gate {
16377c478bd9Sstevel@tonic-gate ulwp->ul_dead = 1;
16387c478bd9Sstevel@tonic-gate ulwp->ul_lwpid = (lwpid_t)(-1);
16397c478bd9Sstevel@tonic-gate ulwp->ul_hash = NULL;
16407c478bd9Sstevel@tonic-gate ulwp->ul_ix = -1;
16417c478bd9Sstevel@tonic-gate ulwp->ul_schedctl = NULL;
16427c478bd9Sstevel@tonic-gate ulwp->ul_schedctl_called = NULL;
16437c478bd9Sstevel@tonic-gate }
16447c478bd9Sstevel@tonic-gate
16457c478bd9Sstevel@tonic-gate /*
16467c478bd9Sstevel@tonic-gate * This is called from fork1() in the child.
16477c478bd9Sstevel@tonic-gate * Reset our data structures to reflect one lwp.
16487c478bd9Sstevel@tonic-gate */
16497c478bd9Sstevel@tonic-gate void
postfork1_child()1650f841f6adSraf postfork1_child()
16517c478bd9Sstevel@tonic-gate {
16527c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
16537c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
1654d4204c85Sraf queue_head_t *qp;
16557c478bd9Sstevel@tonic-gate ulwp_t *next;
16567c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
16577c478bd9Sstevel@tonic-gate int i;
16587c478bd9Sstevel@tonic-gate
16597c478bd9Sstevel@tonic-gate /* daemon threads shouldn't call fork1(), but oh well... */
16607c478bd9Sstevel@tonic-gate self->ul_usropts &= ~THR_DAEMON;
16617c478bd9Sstevel@tonic-gate udp->nthreads = 1;
16627c478bd9Sstevel@tonic-gate udp->ndaemons = 0;
16637c478bd9Sstevel@tonic-gate udp->uberflags.uf_mt = 0;
16641d2738a5Sraf __libc_threaded = 0;
16657c478bd9Sstevel@tonic-gate for (i = 0; i < udp->hash_size; i++)
16667c478bd9Sstevel@tonic-gate udp->thr_hash_table[i].hash_bucket = NULL;
16677257d1b4Sraf self->ul_lwpid = _lwp_self();
16687c478bd9Sstevel@tonic-gate hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
16697c478bd9Sstevel@tonic-gate
167098c1a6b4Sraf /*
16718cd45542Sraf * Some thread in the parent might have been suspended
16728cd45542Sraf * while holding udp->callout_lock or udp->ld_lock.
16738cd45542Sraf * Reinitialize the child's copies.
167498c1a6b4Sraf */
16757257d1b4Sraf (void) mutex_init(&udp->callout_lock,
16767257d1b4Sraf USYNC_THREAD | LOCK_RECURSIVE, NULL);
16777257d1b4Sraf (void) mutex_init(&udp->ld_lock,
16787257d1b4Sraf USYNC_THREAD | LOCK_RECURSIVE, NULL);
167998c1a6b4Sraf
16807c478bd9Sstevel@tonic-gate /* no one in the child is on a sleep queue; reinitialize */
1681d4204c85Sraf if ((qp = udp->queue_head) != NULL) {
16828cd45542Sraf (void) memset(qp, 0, 2 * QHASHSIZE * sizeof (queue_head_t));
1683d4204c85Sraf for (i = 0; i < 2 * QHASHSIZE; qp++, i++) {
1684d4204c85Sraf qp->qh_type = (i < QHASHSIZE)? MX : CV;
1685d4204c85Sraf qp->qh_lock.mutex_flag = LOCK_INITED;
1686d4204c85Sraf qp->qh_lock.mutex_magic = MUTEX_MAGIC;
1687d4204c85Sraf qp->qh_hlist = &qp->qh_def_root;
1688d4204c85Sraf #if defined(THREAD_DEBUG)
1689d4204c85Sraf qp->qh_hlen = 1;
1690d4204c85Sraf qp->qh_hmax = 1;
1691d4204c85Sraf #endif
1692883492d5Sraf }
16937c478bd9Sstevel@tonic-gate }
16947c478bd9Sstevel@tonic-gate
1695c4a8d66cSRoger A. Faulkner /*
1696c4a8d66cSRoger A. Faulkner * Do post-fork1 processing for subsystems that need it.
1697c4a8d66cSRoger A. Faulkner * We need to do this before unmapping all of the abandoned
1698c4a8d66cSRoger A. Faulkner * threads' stacks, below(), because the post-fork1 actions
1699c4a8d66cSRoger A. Faulkner * might require access to those stacks.
1700c4a8d66cSRoger A. Faulkner */
1701c4a8d66cSRoger A. Faulkner postfork1_child_sigev_aio();
1702c4a8d66cSRoger A. Faulkner postfork1_child_sigev_mq();
1703c4a8d66cSRoger A. Faulkner postfork1_child_sigev_timer();
1704c4a8d66cSRoger A. Faulkner postfork1_child_aio();
1705c4a8d66cSRoger A. Faulkner /*
1706c4a8d66cSRoger A. Faulkner * The above subsystems use thread pools, so this action
1707c4a8d66cSRoger A. Faulkner * must be performed after those actions.
1708c4a8d66cSRoger A. Faulkner */
1709c4a8d66cSRoger A. Faulkner postfork1_child_tpool();
1710c4a8d66cSRoger A. Faulkner
17117c478bd9Sstevel@tonic-gate /*
17127c478bd9Sstevel@tonic-gate * All lwps except ourself are gone. Mark them so.
17137c478bd9Sstevel@tonic-gate * First mark all of the lwps that have already been freed.
17147c478bd9Sstevel@tonic-gate * Then mark and free all of the active lwps except ourself.
17157c478bd9Sstevel@tonic-gate * Since we are single-threaded, no locks are required here.
17167c478bd9Sstevel@tonic-gate */
17177c478bd9Sstevel@tonic-gate for (ulwp = udp->lwp_stacks; ulwp != NULL; ulwp = ulwp->ul_next)
17187c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp);
17197c478bd9Sstevel@tonic-gate for (ulwp = udp->ulwp_freelist; ulwp != NULL; ulwp = ulwp->ul_next)
17207c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp);
17217c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = next) {
17227c478bd9Sstevel@tonic-gate next = ulwp->ul_forw;
17237c478bd9Sstevel@tonic-gate ulwp->ul_forw = ulwp->ul_back = NULL;
17247c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp);
17257c478bd9Sstevel@tonic-gate tsd_free(ulwp);
17267c478bd9Sstevel@tonic-gate tls_free(ulwp);
17277c478bd9Sstevel@tonic-gate rwl_free(ulwp);
1728883492d5Sraf heldlock_free(ulwp);
17297c478bd9Sstevel@tonic-gate ulwp_free(ulwp);
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate self->ul_forw = self->ul_back = udp->all_lwps = self;
17327c478bd9Sstevel@tonic-gate if (self != udp->ulwp_one)
17337c478bd9Sstevel@tonic-gate mark_dead_and_buried(udp->ulwp_one);
17347c478bd9Sstevel@tonic-gate if ((ulwp = udp->all_zombies) != NULL) {
17357c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies != 0);
17367c478bd9Sstevel@tonic-gate do {
17377c478bd9Sstevel@tonic-gate next = ulwp->ul_forw;
17387c478bd9Sstevel@tonic-gate ulwp->ul_forw = ulwp->ul_back = NULL;
17397c478bd9Sstevel@tonic-gate mark_dead_and_buried(ulwp);
17407c478bd9Sstevel@tonic-gate udp->nzombies--;
17417c478bd9Sstevel@tonic-gate if (ulwp->ul_replace) {
17427c478bd9Sstevel@tonic-gate ulwp->ul_next = NULL;
17437c478bd9Sstevel@tonic-gate if (udp->ulwp_replace_free == NULL) {
17447c478bd9Sstevel@tonic-gate udp->ulwp_replace_free =
17452e145884Sraf udp->ulwp_replace_last = ulwp;
17467c478bd9Sstevel@tonic-gate } else {
17477c478bd9Sstevel@tonic-gate udp->ulwp_replace_last->ul_next = ulwp;
17487c478bd9Sstevel@tonic-gate udp->ulwp_replace_last = ulwp;
17497c478bd9Sstevel@tonic-gate }
17507c478bd9Sstevel@tonic-gate }
17517c478bd9Sstevel@tonic-gate } while ((ulwp = next) != udp->all_zombies);
17527c478bd9Sstevel@tonic-gate ASSERT(udp->nzombies == 0);
17537c478bd9Sstevel@tonic-gate udp->all_zombies = NULL;
17547c478bd9Sstevel@tonic-gate udp->nzombies = 0;
17557c478bd9Sstevel@tonic-gate }
17567c478bd9Sstevel@tonic-gate trim_stack_cache(0);
17577c478bd9Sstevel@tonic-gate }
17587c478bd9Sstevel@tonic-gate
17597c478bd9Sstevel@tonic-gate lwpid_t
lwp_self(void)17607c478bd9Sstevel@tonic-gate lwp_self(void)
17617c478bd9Sstevel@tonic-gate {
17627c478bd9Sstevel@tonic-gate return (curthread->ul_lwpid);
17637c478bd9Sstevel@tonic-gate }
17647c478bd9Sstevel@tonic-gate
17657257d1b4Sraf #pragma weak _ti_thr_self = thr_self
17667257d1b4Sraf #pragma weak pthread_self = thr_self
17677c478bd9Sstevel@tonic-gate thread_t
thr_self()17687257d1b4Sraf thr_self()
17697c478bd9Sstevel@tonic-gate {
17707c478bd9Sstevel@tonic-gate return (curthread->ul_lwpid);
17717c478bd9Sstevel@tonic-gate }
17727c478bd9Sstevel@tonic-gate
17737c478bd9Sstevel@tonic-gate int
thr_main()17747257d1b4Sraf thr_main()
17757c478bd9Sstevel@tonic-gate {
17767c478bd9Sstevel@tonic-gate ulwp_t *self = __curthread();
17777c478bd9Sstevel@tonic-gate
17787c478bd9Sstevel@tonic-gate return ((self == NULL)? -1 : self->ul_main);
17797c478bd9Sstevel@tonic-gate }
17807c478bd9Sstevel@tonic-gate
1781657b1f3dSraf int
_thrp_cancelled(void)1782657b1f3dSraf _thrp_cancelled(void)
1783657b1f3dSraf {
1784657b1f3dSraf return (curthread->ul_rval == PTHREAD_CANCELED);
1785657b1f3dSraf }
1786657b1f3dSraf
17877c478bd9Sstevel@tonic-gate int
_thrp_stksegment(ulwp_t * ulwp,stack_t * stk)17887c478bd9Sstevel@tonic-gate _thrp_stksegment(ulwp_t *ulwp, stack_t *stk)
17897c478bd9Sstevel@tonic-gate {
17907c478bd9Sstevel@tonic-gate stk->ss_sp = (void *)ulwp->ul_stktop;
17917c478bd9Sstevel@tonic-gate stk->ss_size = ulwp->ul_stksiz;
17927c478bd9Sstevel@tonic-gate stk->ss_flags = 0;
17937c478bd9Sstevel@tonic-gate return (0);
17947c478bd9Sstevel@tonic-gate }
17957c478bd9Sstevel@tonic-gate
17967257d1b4Sraf #pragma weak _thr_stksegment = thr_stksegment
17977c478bd9Sstevel@tonic-gate int
thr_stksegment(stack_t * stk)17987257d1b4Sraf thr_stksegment(stack_t *stk)
17997c478bd9Sstevel@tonic-gate {
18007c478bd9Sstevel@tonic-gate return (_thrp_stksegment(curthread, stk));
18017c478bd9Sstevel@tonic-gate }
18027c478bd9Sstevel@tonic-gate
18037c478bd9Sstevel@tonic-gate void
force_continue(ulwp_t * ulwp)18047c478bd9Sstevel@tonic-gate force_continue(ulwp_t *ulwp)
18057c478bd9Sstevel@tonic-gate {
18067c478bd9Sstevel@tonic-gate #if defined(THREAD_DEBUG)
18077c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
18087c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
18097c478bd9Sstevel@tonic-gate #endif
18107c478bd9Sstevel@tonic-gate int error;
18117c478bd9Sstevel@tonic-gate timespec_t ts;
18127c478bd9Sstevel@tonic-gate
181336319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
18147c478bd9Sstevel@tonic-gate ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
18157c478bd9Sstevel@tonic-gate
18167c478bd9Sstevel@tonic-gate for (;;) {
18177257d1b4Sraf error = _lwp_continue(ulwp->ul_lwpid);
18187c478bd9Sstevel@tonic-gate if (error != 0 && error != EINTR)
18197c478bd9Sstevel@tonic-gate break;
18207c478bd9Sstevel@tonic-gate error = 0;
1821a141dbd6SAlison C if (ulwp->ul_stopping) { /* it is stopping itself */
182248bbca81SDaniel Hoffman ts.tv_sec = 0; /* give it a chance to run */
18237c478bd9Sstevel@tonic-gate ts.tv_nsec = 100000; /* 100 usecs or clock tick */
1824f841f6adSraf (void) __nanosleep(&ts, NULL);
18257c478bd9Sstevel@tonic-gate }
182648bbca81SDaniel Hoffman if (!ulwp->ul_stopping) /* it is running now */
18277c478bd9Sstevel@tonic-gate break; /* so we are done */
18287c478bd9Sstevel@tonic-gate /*
182948bbca81SDaniel Hoffman * It is marked as being in the process of stopping
183048bbca81SDaniel Hoffman * itself. Loop around and continue it again.
183148bbca81SDaniel Hoffman * It may not have been stopped the first time.
18327c478bd9Sstevel@tonic-gate */
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate }
18357c478bd9Sstevel@tonic-gate
18367c478bd9Sstevel@tonic-gate /*
1837e54ab87fSRoger A. Faulkner * Suspend an lwp with lwp_suspend(), then move it to a safe point,
1838e54ab87fSRoger A. Faulkner * that is, to a point where ul_critical and ul_rtld are both zero.
18397c478bd9Sstevel@tonic-gate * On return, the ulwp_lock() is dropped as with ulwp_unlock().
18407c478bd9Sstevel@tonic-gate * If 'link_dropped' is non-NULL, then 'link_lock' is held on entry.
18417c478bd9Sstevel@tonic-gate * If we have to drop link_lock, we store 1 through link_dropped.
18427c478bd9Sstevel@tonic-gate * If the lwp exits before it can be suspended, we return ESRCH.
18437c478bd9Sstevel@tonic-gate */
18447c478bd9Sstevel@tonic-gate int
safe_suspend(ulwp_t * ulwp,uchar_t whystopped,int * link_dropped)18457c478bd9Sstevel@tonic-gate safe_suspend(ulwp_t *ulwp, uchar_t whystopped, int *link_dropped)
18467c478bd9Sstevel@tonic-gate {
18477c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
18487c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
18497c478bd9Sstevel@tonic-gate cond_t *cvp = ulwp_condvar(ulwp, udp);
18507c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp);
18517c478bd9Sstevel@tonic-gate thread_t tid = ulwp->ul_lwpid;
18527c478bd9Sstevel@tonic-gate int ix = ulwp->ul_ix;
18537c478bd9Sstevel@tonic-gate int error = 0;
18547c478bd9Sstevel@tonic-gate
18557c478bd9Sstevel@tonic-gate ASSERT(whystopped == TSTP_REGULAR ||
18567c478bd9Sstevel@tonic-gate whystopped == TSTP_MUTATOR ||
18577c478bd9Sstevel@tonic-gate whystopped == TSTP_FORK);
18587c478bd9Sstevel@tonic-gate ASSERT(ulwp != self);
18597c478bd9Sstevel@tonic-gate ASSERT(!ulwp->ul_stop);
186036319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
18617c478bd9Sstevel@tonic-gate ASSERT(MUTEX_OWNED(mp, self));
18627c478bd9Sstevel@tonic-gate
18637c478bd9Sstevel@tonic-gate if (link_dropped != NULL)
18647c478bd9Sstevel@tonic-gate *link_dropped = 0;
18657c478bd9Sstevel@tonic-gate
18667c478bd9Sstevel@tonic-gate /*
18677c478bd9Sstevel@tonic-gate * We must grab the target's spin lock before suspending it.
18687c478bd9Sstevel@tonic-gate * See the comments below and in _thrp_suspend() for why.
18697c478bd9Sstevel@tonic-gate */
18707c478bd9Sstevel@tonic-gate spin_lock_set(&ulwp->ul_spinlock);
18717c478bd9Sstevel@tonic-gate (void) ___lwp_suspend(tid);
18727c478bd9Sstevel@tonic-gate spin_lock_clear(&ulwp->ul_spinlock);
18737c478bd9Sstevel@tonic-gate
18747c478bd9Sstevel@tonic-gate top:
1875e54ab87fSRoger A. Faulkner if ((ulwp->ul_critical == 0 && ulwp->ul_rtld == 0) ||
1876e54ab87fSRoger A. Faulkner ulwp->ul_stopping) {
18777c478bd9Sstevel@tonic-gate /* thread is already safe */
18787c478bd9Sstevel@tonic-gate ulwp->ul_stop |= whystopped;
18797c478bd9Sstevel@tonic-gate } else {
18807c478bd9Sstevel@tonic-gate /*
18817c478bd9Sstevel@tonic-gate * Setting ul_pleasestop causes the target thread to stop
18827c478bd9Sstevel@tonic-gate * itself in _thrp_suspend(), below, after we drop its lock.
18837c478bd9Sstevel@tonic-gate * We must continue the critical thread before dropping
18847c478bd9Sstevel@tonic-gate * link_lock because the critical thread may be holding
18857c478bd9Sstevel@tonic-gate * the queue lock for link_lock. This is delicate.
18867c478bd9Sstevel@tonic-gate */
18877c478bd9Sstevel@tonic-gate ulwp->ul_pleasestop |= whystopped;
18887c478bd9Sstevel@tonic-gate force_continue(ulwp);
18897c478bd9Sstevel@tonic-gate if (link_dropped != NULL) {
18907c478bd9Sstevel@tonic-gate *link_dropped = 1;
18917c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
18927c478bd9Sstevel@tonic-gate /* be sure to drop link_lock only once */
18937c478bd9Sstevel@tonic-gate link_dropped = NULL;
18947c478bd9Sstevel@tonic-gate }
18957c478bd9Sstevel@tonic-gate
18967c478bd9Sstevel@tonic-gate /*
18977c478bd9Sstevel@tonic-gate * The thread may disappear by calling thr_exit() so we
18987c478bd9Sstevel@tonic-gate * cannot rely on the ulwp pointer after dropping the lock.
18997c478bd9Sstevel@tonic-gate * Instead, we search the hash table to find it again.
19007c478bd9Sstevel@tonic-gate * When we return, we may find that the thread has been
19017c478bd9Sstevel@tonic-gate * continued by some other thread. The suspend/continue
19027c478bd9Sstevel@tonic-gate * interfaces are prone to such race conditions by design.
19037c478bd9Sstevel@tonic-gate */
19047c478bd9Sstevel@tonic-gate while (ulwp && !ulwp->ul_dead && !ulwp->ul_stop &&
19057c478bd9Sstevel@tonic-gate (ulwp->ul_pleasestop & whystopped)) {
1906a574db85Sraf (void) __cond_wait(cvp, mp);
19077c478bd9Sstevel@tonic-gate for (ulwp = udp->thr_hash_table[ix].hash_bucket;
19087c478bd9Sstevel@tonic-gate ulwp != NULL; ulwp = ulwp->ul_hash) {
19097c478bd9Sstevel@tonic-gate if (ulwp->ul_lwpid == tid)
19107c478bd9Sstevel@tonic-gate break;
19117c478bd9Sstevel@tonic-gate }
19127c478bd9Sstevel@tonic-gate }
19137c478bd9Sstevel@tonic-gate
19147c478bd9Sstevel@tonic-gate if (ulwp == NULL || ulwp->ul_dead)
19157c478bd9Sstevel@tonic-gate error = ESRCH;
19167c478bd9Sstevel@tonic-gate else {
19177c478bd9Sstevel@tonic-gate /*
19187c478bd9Sstevel@tonic-gate * Do another lwp_suspend() to make sure we don't
19197c478bd9Sstevel@tonic-gate * return until the target thread is fully stopped
19207c478bd9Sstevel@tonic-gate * in the kernel. Don't apply lwp_suspend() until
19217c478bd9Sstevel@tonic-gate * we know that the target is not holding any
19227c478bd9Sstevel@tonic-gate * queue locks, that is, that it has completed
19237c478bd9Sstevel@tonic-gate * ulwp_unlock(self) and has, or at least is
19247c478bd9Sstevel@tonic-gate * about to, call lwp_suspend() on itself. We do
19257c478bd9Sstevel@tonic-gate * this by grabbing the target's spin lock.
19267c478bd9Sstevel@tonic-gate */
19277c478bd9Sstevel@tonic-gate ASSERT(ulwp->ul_lwpid == tid);
19287c478bd9Sstevel@tonic-gate spin_lock_set(&ulwp->ul_spinlock);
19297c478bd9Sstevel@tonic-gate (void) ___lwp_suspend(tid);
19307c478bd9Sstevel@tonic-gate spin_lock_clear(&ulwp->ul_spinlock);
19317c478bd9Sstevel@tonic-gate /*
19327c478bd9Sstevel@tonic-gate * If some other thread did a thr_continue()
19337c478bd9Sstevel@tonic-gate * on the target thread we have to start over.
19347c478bd9Sstevel@tonic-gate */
19357c478bd9Sstevel@tonic-gate if (!ulwp->ul_stopping || !(ulwp->ul_stop & whystopped))
19367c478bd9Sstevel@tonic-gate goto top;
19377c478bd9Sstevel@tonic-gate }
19387c478bd9Sstevel@tonic-gate }
19397c478bd9Sstevel@tonic-gate
19407257d1b4Sraf (void) cond_broadcast(cvp);
19417c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
19427c478bd9Sstevel@tonic-gate return (error);
19437c478bd9Sstevel@tonic-gate }
19447c478bd9Sstevel@tonic-gate
19457c478bd9Sstevel@tonic-gate int
_thrp_suspend(thread_t tid,uchar_t whystopped)19467c478bd9Sstevel@tonic-gate _thrp_suspend(thread_t tid, uchar_t whystopped)
19477c478bd9Sstevel@tonic-gate {
19487c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
19497c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
19507c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
19517c478bd9Sstevel@tonic-gate int error = 0;
19527c478bd9Sstevel@tonic-gate
19537c478bd9Sstevel@tonic-gate ASSERT((whystopped & (TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) != 0);
19547c478bd9Sstevel@tonic-gate ASSERT((whystopped & ~(TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) == 0);
19557c478bd9Sstevel@tonic-gate
19567c478bd9Sstevel@tonic-gate /*
19577f6e74f8Sraf * We can't suspend anyone except ourself while
19587f6e74f8Sraf * some other thread is performing a fork.
19597f6e74f8Sraf * This also allows only one suspension at a time.
19607c478bd9Sstevel@tonic-gate */
19617f6e74f8Sraf if (tid != self->ul_lwpid)
19622e145884Sraf fork_lock_enter();
19637c478bd9Sstevel@tonic-gate
19647c478bd9Sstevel@tonic-gate if ((ulwp = find_lwp(tid)) == NULL)
19657c478bd9Sstevel@tonic-gate error = ESRCH;
19667c478bd9Sstevel@tonic-gate else if (whystopped == TSTP_MUTATOR && !ulwp->ul_mutator) {
19677c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
19687c478bd9Sstevel@tonic-gate error = EINVAL;
19697c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop) { /* already stopped */
19707c478bd9Sstevel@tonic-gate ulwp->ul_stop |= whystopped;
19717c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
19727c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
19737c478bd9Sstevel@tonic-gate } else if (ulwp != self) {
19747c478bd9Sstevel@tonic-gate /*
19757c478bd9Sstevel@tonic-gate * After suspending the other thread, move it out of a
19767c478bd9Sstevel@tonic-gate * critical section and deal with the schedctl mappings.
19777c478bd9Sstevel@tonic-gate * safe_suspend() suspends the other thread, calls
19787c478bd9Sstevel@tonic-gate * ulwp_broadcast(ulwp) and drops the ulwp lock.
19797c478bd9Sstevel@tonic-gate */
19807c478bd9Sstevel@tonic-gate error = safe_suspend(ulwp, whystopped, NULL);
19817c478bd9Sstevel@tonic-gate } else {
19827c478bd9Sstevel@tonic-gate int schedctl_after_fork = 0;
19837c478bd9Sstevel@tonic-gate
19847c478bd9Sstevel@tonic-gate /*
19857c478bd9Sstevel@tonic-gate * We are suspending ourself. We must not take a signal
19867c478bd9Sstevel@tonic-gate * until we return from lwp_suspend() and clear ul_stopping.
19877c478bd9Sstevel@tonic-gate * This is to guard against siglongjmp().
19887c478bd9Sstevel@tonic-gate */
19897c478bd9Sstevel@tonic-gate enter_critical(self);
19907c478bd9Sstevel@tonic-gate self->ul_sp = stkptr();
19917c478bd9Sstevel@tonic-gate _flush_windows(); /* sparc */
19927c478bd9Sstevel@tonic-gate self->ul_pleasestop = 0;
19937c478bd9Sstevel@tonic-gate self->ul_stop |= whystopped;
19947c478bd9Sstevel@tonic-gate /*
19957c478bd9Sstevel@tonic-gate * Grab our spin lock before dropping ulwp_mutex(self).
19967c478bd9Sstevel@tonic-gate * This prevents the suspending thread from applying
19977c478bd9Sstevel@tonic-gate * lwp_suspend() to us before we emerge from
19987c478bd9Sstevel@tonic-gate * lmutex_unlock(mp) and have dropped mp's queue lock.
19997c478bd9Sstevel@tonic-gate */
20007c478bd9Sstevel@tonic-gate spin_lock_set(&self->ul_spinlock);
20017c478bd9Sstevel@tonic-gate self->ul_stopping = 1;
20027c478bd9Sstevel@tonic-gate ulwp_broadcast(self);
20037c478bd9Sstevel@tonic-gate ulwp_unlock(self, udp);
20047c478bd9Sstevel@tonic-gate /*
20057c478bd9Sstevel@tonic-gate * From this point until we return from lwp_suspend(),
20067c478bd9Sstevel@tonic-gate * we must not call any function that might invoke the
20077c478bd9Sstevel@tonic-gate * dynamic linker, that is, we can only call functions
20087c478bd9Sstevel@tonic-gate * private to the library.
20097c478bd9Sstevel@tonic-gate *
20107c478bd9Sstevel@tonic-gate * Also, this is a nasty race condition for a process
20117c478bd9Sstevel@tonic-gate * that is undergoing a forkall() operation:
20127c478bd9Sstevel@tonic-gate * Once we clear our spinlock (below), we are vulnerable
20137c478bd9Sstevel@tonic-gate * to being suspended by the forkall() thread before
20147c478bd9Sstevel@tonic-gate * we manage to suspend ourself in ___lwp_suspend().
20157c478bd9Sstevel@tonic-gate * See safe_suspend() and force_continue().
20167c478bd9Sstevel@tonic-gate *
20177c478bd9Sstevel@tonic-gate * To avoid a SIGSEGV due to the disappearance
20187c478bd9Sstevel@tonic-gate * of the schedctl mappings in the child process,
20197c478bd9Sstevel@tonic-gate * which can happen in spin_lock_clear() if we
20207c478bd9Sstevel@tonic-gate * are suspended while we are in the middle of
20217c478bd9Sstevel@tonic-gate * its call to preempt(), we preemptively clear
20227c478bd9Sstevel@tonic-gate * our own schedctl pointer before dropping our
20237c478bd9Sstevel@tonic-gate * spinlock. We reinstate it, in both the parent
20247c478bd9Sstevel@tonic-gate * and (if this really is a forkall()) the child.
20257c478bd9Sstevel@tonic-gate */
20267c478bd9Sstevel@tonic-gate if (whystopped & TSTP_FORK) {
20277c478bd9Sstevel@tonic-gate schedctl_after_fork = 1;
20287c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL;
20297c478bd9Sstevel@tonic-gate self->ul_schedctl_called = &udp->uberflags;
20307c478bd9Sstevel@tonic-gate }
20317c478bd9Sstevel@tonic-gate spin_lock_clear(&self->ul_spinlock);
20327c478bd9Sstevel@tonic-gate (void) ___lwp_suspend(tid);
20337c478bd9Sstevel@tonic-gate /*
20347c478bd9Sstevel@tonic-gate * Somebody else continued us.
20357c478bd9Sstevel@tonic-gate * We can't grab ulwp_lock(self)
20367c478bd9Sstevel@tonic-gate * until after clearing ul_stopping.
20377c478bd9Sstevel@tonic-gate * force_continue() relies on this.
20387c478bd9Sstevel@tonic-gate */
20397c478bd9Sstevel@tonic-gate self->ul_stopping = 0;
20407c478bd9Sstevel@tonic-gate self->ul_sp = 0;
20417c478bd9Sstevel@tonic-gate if (schedctl_after_fork) {
20427c478bd9Sstevel@tonic-gate self->ul_schedctl_called = NULL;
20437c478bd9Sstevel@tonic-gate self->ul_schedctl = NULL;
20447c478bd9Sstevel@tonic-gate (void) setup_schedctl();
20457c478bd9Sstevel@tonic-gate }
20467c478bd9Sstevel@tonic-gate ulwp_lock(self, udp);
20477c478bd9Sstevel@tonic-gate ulwp_broadcast(self);
20487c478bd9Sstevel@tonic-gate ulwp_unlock(self, udp);
20497c478bd9Sstevel@tonic-gate exit_critical(self);
20507c478bd9Sstevel@tonic-gate }
20517c478bd9Sstevel@tonic-gate
20527c478bd9Sstevel@tonic-gate if (tid != self->ul_lwpid)
20537c478bd9Sstevel@tonic-gate fork_lock_exit();
20547c478bd9Sstevel@tonic-gate
20557c478bd9Sstevel@tonic-gate return (error);
20567c478bd9Sstevel@tonic-gate }
20577c478bd9Sstevel@tonic-gate
20587c478bd9Sstevel@tonic-gate /*
20597c478bd9Sstevel@tonic-gate * Suspend all lwps other than ourself in preparation for fork.
20607c478bd9Sstevel@tonic-gate */
20617c478bd9Sstevel@tonic-gate void
suspend_fork()20627c478bd9Sstevel@tonic-gate suspend_fork()
20637c478bd9Sstevel@tonic-gate {
20647c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
20657c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
20667c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
20677c478bd9Sstevel@tonic-gate int link_dropped;
20687c478bd9Sstevel@tonic-gate
206936319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
20707c478bd9Sstevel@tonic-gate top:
20717c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
20727c478bd9Sstevel@tonic-gate
20737c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
20747c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp);
20757c478bd9Sstevel@tonic-gate if (ulwp->ul_stop) { /* already stopped */
20767c478bd9Sstevel@tonic-gate ulwp->ul_stop |= TSTP_FORK;
20777c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
20787c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
20797c478bd9Sstevel@tonic-gate } else {
20807c478bd9Sstevel@tonic-gate /*
20817c478bd9Sstevel@tonic-gate * Move the stopped lwp out of a critical section.
20827c478bd9Sstevel@tonic-gate */
20837c478bd9Sstevel@tonic-gate if (safe_suspend(ulwp, TSTP_FORK, &link_dropped) ||
20847c478bd9Sstevel@tonic-gate link_dropped)
20857c478bd9Sstevel@tonic-gate goto top;
20867c478bd9Sstevel@tonic-gate }
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate
20897c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
20907c478bd9Sstevel@tonic-gate }
20917c478bd9Sstevel@tonic-gate
20927c478bd9Sstevel@tonic-gate void
continue_fork(int child)20937c478bd9Sstevel@tonic-gate continue_fork(int child)
20947c478bd9Sstevel@tonic-gate {
20957c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
20967c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
20977c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
20987c478bd9Sstevel@tonic-gate
209936319254Sraf ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
210036319254Sraf
21017c478bd9Sstevel@tonic-gate /*
21027c478bd9Sstevel@tonic-gate * Clear the schedctl pointers in the child of forkall().
21037c478bd9Sstevel@tonic-gate */
21047c478bd9Sstevel@tonic-gate if (child) {
21057c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
21067c478bd9Sstevel@tonic-gate ulwp->ul_schedctl_called =
21072e145884Sraf ulwp->ul_dead? &udp->uberflags : NULL;
21087c478bd9Sstevel@tonic-gate ulwp->ul_schedctl = NULL;
21097c478bd9Sstevel@tonic-gate }
21107c478bd9Sstevel@tonic-gate }
21117c478bd9Sstevel@tonic-gate
21127c478bd9Sstevel@tonic-gate /*
21137c478bd9Sstevel@tonic-gate * Set all lwps that were stopped for fork() running again.
21147c478bd9Sstevel@tonic-gate */
21157c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
21167c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
21177c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp);
21187c478bd9Sstevel@tonic-gate lmutex_lock(mp);
21197c478bd9Sstevel@tonic-gate ASSERT(ulwp->ul_stop & TSTP_FORK);
21207c478bd9Sstevel@tonic-gate ulwp->ul_stop &= ~TSTP_FORK;
21217c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
21227c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop)
21237c478bd9Sstevel@tonic-gate force_continue(ulwp);
21247c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
21257c478bd9Sstevel@tonic-gate }
21267c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
21277c478bd9Sstevel@tonic-gate }
21287c478bd9Sstevel@tonic-gate
21297c478bd9Sstevel@tonic-gate int
_thrp_continue(thread_t tid,uchar_t whystopped)21307c478bd9Sstevel@tonic-gate _thrp_continue(thread_t tid, uchar_t whystopped)
21317c478bd9Sstevel@tonic-gate {
21327c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
21337c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
21347c478bd9Sstevel@tonic-gate mutex_t *mp;
21357c478bd9Sstevel@tonic-gate int error = 0;
21367c478bd9Sstevel@tonic-gate
21377c478bd9Sstevel@tonic-gate ASSERT(whystopped == TSTP_REGULAR ||
21387c478bd9Sstevel@tonic-gate whystopped == TSTP_MUTATOR);
21397c478bd9Sstevel@tonic-gate
214036319254Sraf /*
214136319254Sraf * We single-thread the entire thread suspend/continue mechanism.
214236319254Sraf */
21432e145884Sraf fork_lock_enter();
214436319254Sraf
214536319254Sraf if ((ulwp = find_lwp(tid)) == NULL) {
214636319254Sraf fork_lock_exit();
21477c478bd9Sstevel@tonic-gate return (ESRCH);
214836319254Sraf }
21497c478bd9Sstevel@tonic-gate
21507c478bd9Sstevel@tonic-gate mp = ulwp_mutex(ulwp, udp);
21517c478bd9Sstevel@tonic-gate if ((whystopped == TSTP_MUTATOR && !ulwp->ul_mutator)) {
21527c478bd9Sstevel@tonic-gate error = EINVAL;
21537c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop & whystopped) {
21547c478bd9Sstevel@tonic-gate ulwp->ul_stop &= ~whystopped;
21557c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
21567c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop) {
21577c478bd9Sstevel@tonic-gate if (whystopped == TSTP_REGULAR && ulwp->ul_created) {
21587c478bd9Sstevel@tonic-gate ulwp->ul_sp = 0;
21597c478bd9Sstevel@tonic-gate ulwp->ul_created = 0;
21607c478bd9Sstevel@tonic-gate }
21617c478bd9Sstevel@tonic-gate force_continue(ulwp);
21627c478bd9Sstevel@tonic-gate }
21637c478bd9Sstevel@tonic-gate }
21647c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
216536319254Sraf
216636319254Sraf fork_lock_exit();
21677c478bd9Sstevel@tonic-gate return (error);
21687c478bd9Sstevel@tonic-gate }
21697c478bd9Sstevel@tonic-gate
21707c478bd9Sstevel@tonic-gate int
thr_suspend(thread_t tid)21717257d1b4Sraf thr_suspend(thread_t tid)
21727c478bd9Sstevel@tonic-gate {
21737c478bd9Sstevel@tonic-gate return (_thrp_suspend(tid, TSTP_REGULAR));
21747c478bd9Sstevel@tonic-gate }
21757c478bd9Sstevel@tonic-gate
21767c478bd9Sstevel@tonic-gate int
thr_continue(thread_t tid)21777257d1b4Sraf thr_continue(thread_t tid)
21787c478bd9Sstevel@tonic-gate {
21797c478bd9Sstevel@tonic-gate return (_thrp_continue(tid, TSTP_REGULAR));
21807c478bd9Sstevel@tonic-gate }
21817c478bd9Sstevel@tonic-gate
21827c478bd9Sstevel@tonic-gate void
thr_yield()21837257d1b4Sraf thr_yield()
21847c478bd9Sstevel@tonic-gate {
21858cd45542Sraf yield();
21867c478bd9Sstevel@tonic-gate }
21877c478bd9Sstevel@tonic-gate
21887257d1b4Sraf #pragma weak pthread_kill = thr_kill
21897257d1b4Sraf #pragma weak _thr_kill = thr_kill
21907c478bd9Sstevel@tonic-gate int
thr_kill(thread_t tid,int sig)21917257d1b4Sraf thr_kill(thread_t tid, int sig)
21927c478bd9Sstevel@tonic-gate {
21937c478bd9Sstevel@tonic-gate if (sig == SIGCANCEL)
21947c478bd9Sstevel@tonic-gate return (EINVAL);
21957257d1b4Sraf return (_lwp_kill(tid, sig));
21967c478bd9Sstevel@tonic-gate }
21977c478bd9Sstevel@tonic-gate
21987c478bd9Sstevel@tonic-gate /*
21997c478bd9Sstevel@tonic-gate * Exit a critical section, take deferred actions if necessary.
2200e54ab87fSRoger A. Faulkner * Called from exit_critical() and from sigon().
22017c478bd9Sstevel@tonic-gate */
22027c478bd9Sstevel@tonic-gate void
do_exit_critical()22037c478bd9Sstevel@tonic-gate do_exit_critical()
22047c478bd9Sstevel@tonic-gate {
22057c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
22067c478bd9Sstevel@tonic-gate int sig;
22077c478bd9Sstevel@tonic-gate
22087c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 0);
2209e54ab87fSRoger A. Faulkner
2210e54ab87fSRoger A. Faulkner /*
2211e54ab87fSRoger A. Faulkner * Don't suspend ourself or take a deferred signal while dying
2212e54ab87fSRoger A. Faulkner * or while executing inside the dynamic linker (ld.so.1).
2213e54ab87fSRoger A. Faulkner */
2214e54ab87fSRoger A. Faulkner if (self->ul_dead || self->ul_rtld)
22157c478bd9Sstevel@tonic-gate return;
22167c478bd9Sstevel@tonic-gate
22177c478bd9Sstevel@tonic-gate while (self->ul_pleasestop ||
22187c478bd9Sstevel@tonic-gate (self->ul_cursig != 0 && self->ul_sigdefer == 0)) {
22197c478bd9Sstevel@tonic-gate /*
22207c478bd9Sstevel@tonic-gate * Avoid a recursive call to exit_critical() in _thrp_suspend()
22217c478bd9Sstevel@tonic-gate * by keeping self->ul_critical == 1 here.
22227c478bd9Sstevel@tonic-gate */
22237c478bd9Sstevel@tonic-gate self->ul_critical++;
22247c478bd9Sstevel@tonic-gate while (self->ul_pleasestop) {
22257c478bd9Sstevel@tonic-gate /*
22267c478bd9Sstevel@tonic-gate * Guard against suspending ourself while on a sleep
22277c478bd9Sstevel@tonic-gate * queue. See the comments in call_user_handler().
22287c478bd9Sstevel@tonic-gate */
22297c478bd9Sstevel@tonic-gate unsleep_self();
22307c478bd9Sstevel@tonic-gate set_parking_flag(self, 0);
22317c478bd9Sstevel@tonic-gate (void) _thrp_suspend(self->ul_lwpid,
22322e145884Sraf self->ul_pleasestop);
22337c478bd9Sstevel@tonic-gate }
22347c478bd9Sstevel@tonic-gate self->ul_critical--;
22357c478bd9Sstevel@tonic-gate
22367c478bd9Sstevel@tonic-gate if ((sig = self->ul_cursig) != 0 && self->ul_sigdefer == 0) {
22377c478bd9Sstevel@tonic-gate /*
22387c478bd9Sstevel@tonic-gate * Clear ul_cursig before proceeding.
22397c478bd9Sstevel@tonic-gate * This protects us from the dynamic linker's
22407c478bd9Sstevel@tonic-gate * calls to bind_guard()/bind_clear() in the
22417c478bd9Sstevel@tonic-gate * event that it is invoked to resolve a symbol
22427c478bd9Sstevel@tonic-gate * like take_deferred_signal() below.
22437c478bd9Sstevel@tonic-gate */
22447c478bd9Sstevel@tonic-gate self->ul_cursig = 0;
22457c478bd9Sstevel@tonic-gate take_deferred_signal(sig);
22467c478bd9Sstevel@tonic-gate ASSERT(self->ul_cursig == 0);
22477c478bd9Sstevel@tonic-gate }
22487c478bd9Sstevel@tonic-gate }
22497c478bd9Sstevel@tonic-gate ASSERT(self->ul_critical == 0);
22507c478bd9Sstevel@tonic-gate }
22517c478bd9Sstevel@tonic-gate
2252a574db85Sraf /*
2253a574db85Sraf * _ti_bind_guard() and _ti_bind_clear() are called by the dynamic linker
2254a574db85Sraf * (ld.so.1) when it has do do something, like resolve a symbol to be called
2255a574db85Sraf * by the application or one of its libraries. _ti_bind_guard() is called
2256a574db85Sraf * on entry to ld.so.1, _ti_bind_clear() on exit from ld.so.1 back to the
2257a574db85Sraf * application. The dynamic linker gets special dispensation from libc to
2258a574db85Sraf * run in a critical region (all signals deferred and no thread suspension
2259a574db85Sraf * or forking allowed), and to be immune from cancellation for the duration.
2260a574db85Sraf */
22617c478bd9Sstevel@tonic-gate int
_ti_bind_guard(int flags)22628cd45542Sraf _ti_bind_guard(int flags)
22637c478bd9Sstevel@tonic-gate {
22647c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
22658cd45542Sraf uberdata_t *udp = self->ul_uberdata;
22668cd45542Sraf int bindflag = (flags & THR_FLG_RTLD);
22677c478bd9Sstevel@tonic-gate
22687c478bd9Sstevel@tonic-gate if ((self->ul_bindflags & bindflag) == bindflag)
22697c478bd9Sstevel@tonic-gate return (0);
22702a8d6ebaSRod Evans self->ul_bindflags |= bindflag;
22718cd45542Sraf if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
22728cd45542Sraf sigoff(self); /* see no signals while holding ld_lock */
2273e54ab87fSRoger A. Faulkner self->ul_rtld++; /* don't suspend while in ld.so.1 */
22747257d1b4Sraf (void) mutex_lock(&udp->ld_lock);
22758cd45542Sraf }
22767c478bd9Sstevel@tonic-gate enter_critical(self);
2277a574db85Sraf self->ul_save_state = self->ul_cancel_disabled;
2278a574db85Sraf self->ul_cancel_disabled = 1;
2279a574db85Sraf set_cancel_pending_flag(self, 0);
22807c478bd9Sstevel@tonic-gate return (1);
22817c478bd9Sstevel@tonic-gate }
22827c478bd9Sstevel@tonic-gate
22837c478bd9Sstevel@tonic-gate int
_ti_bind_clear(int flags)22848cd45542Sraf _ti_bind_clear(int flags)
22857c478bd9Sstevel@tonic-gate {
22867c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
22878cd45542Sraf uberdata_t *udp = self->ul_uberdata;
22888cd45542Sraf int bindflag = (flags & THR_FLG_RTLD);
22897c478bd9Sstevel@tonic-gate
22907c478bd9Sstevel@tonic-gate if ((self->ul_bindflags & bindflag) == 0)
22917c478bd9Sstevel@tonic-gate return (self->ul_bindflags);
22927c478bd9Sstevel@tonic-gate self->ul_bindflags &= ~bindflag;
2293a574db85Sraf self->ul_cancel_disabled = self->ul_save_state;
2294a574db85Sraf set_cancel_pending_flag(self, 0);
22957c478bd9Sstevel@tonic-gate exit_critical(self);
22968cd45542Sraf if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
22978cd45542Sraf if (MUTEX_OWNED(&udp->ld_lock, self)) {
22987257d1b4Sraf (void) mutex_unlock(&udp->ld_lock);
2299e54ab87fSRoger A. Faulkner self->ul_rtld--;
23008cd45542Sraf sigon(self); /* reenable signals */
23018cd45542Sraf }
23028cd45542Sraf }
23037c478bd9Sstevel@tonic-gate return (self->ul_bindflags);
23047c478bd9Sstevel@tonic-gate }
23057c478bd9Sstevel@tonic-gate
23062a8d6ebaSRod Evans /*
23072a8d6ebaSRod Evans * Tell the dynamic linker (ld.so.1) whether or not it was entered from
23082a8d6ebaSRod Evans * a critical region in libc. Return zero if not, else return non-zero.
23092a8d6ebaSRod Evans */
23102a8d6ebaSRod Evans int
_ti_critical(void)23112a8d6ebaSRod Evans _ti_critical(void)
23122a8d6ebaSRod Evans {
23132a8d6ebaSRod Evans ulwp_t *self = curthread;
23142a8d6ebaSRod Evans int level = self->ul_critical;
23152a8d6ebaSRod Evans
23162a8d6ebaSRod Evans if ((self->ul_bindflags & THR_FLG_RTLD) == 0 || level == 0)
23172a8d6ebaSRod Evans return (level); /* ld.so.1 hasn't (yet) called enter() */
23182a8d6ebaSRod Evans return (level - 1);
23192a8d6ebaSRod Evans }
23202a8d6ebaSRod Evans
23217c478bd9Sstevel@tonic-gate /*
23227c478bd9Sstevel@tonic-gate * sigoff() and sigon() enable cond_wait() to behave (optionally) like
23237c478bd9Sstevel@tonic-gate * it does in the old libthread (see the comments in cond_wait_queue()).
23247c478bd9Sstevel@tonic-gate * Also, signals are deferred at thread startup until TLS constructors
23257257d1b4Sraf * have all been called, at which time _thrp_setup() calls sigon().
232634709573Sraf *
2327f841f6adSraf * _sigoff() and _sigon() are external consolidation-private interfaces to
2328f841f6adSraf * sigoff() and sigon(), respectively, in libc. These are used in libnsl.
23297c478bd9Sstevel@tonic-gate * Also, _sigoff() and _sigon() are called from dbx's run-time checking
23307c478bd9Sstevel@tonic-gate * (librtc.so) to defer signals during its critical sections (not to be
23317c478bd9Sstevel@tonic-gate * confused with libc critical sections [see exit_critical() above]).
23327c478bd9Sstevel@tonic-gate */
23337c478bd9Sstevel@tonic-gate void
_sigoff(void)23347c478bd9Sstevel@tonic-gate _sigoff(void)
23357c478bd9Sstevel@tonic-gate {
2336e54ab87fSRoger A. Faulkner ulwp_t *self = curthread;
23377c478bd9Sstevel@tonic-gate
2338e54ab87fSRoger A. Faulkner sigoff(self);
23397c478bd9Sstevel@tonic-gate }
23407c478bd9Sstevel@tonic-gate
23417c478bd9Sstevel@tonic-gate void
_sigon(void)2342e54ab87fSRoger A. Faulkner _sigon(void)
23437c478bd9Sstevel@tonic-gate {
2344e54ab87fSRoger A. Faulkner ulwp_t *self = curthread;
23457c478bd9Sstevel@tonic-gate
23467c478bd9Sstevel@tonic-gate ASSERT(self->ul_sigdefer > 0);
2347e54ab87fSRoger A. Faulkner sigon(self);
23487c478bd9Sstevel@tonic-gate }
23497c478bd9Sstevel@tonic-gate
23507c478bd9Sstevel@tonic-gate int
thr_getconcurrency()23517257d1b4Sraf thr_getconcurrency()
23527c478bd9Sstevel@tonic-gate {
23537c478bd9Sstevel@tonic-gate return (thr_concurrency);
23547c478bd9Sstevel@tonic-gate }
23557c478bd9Sstevel@tonic-gate
23567c478bd9Sstevel@tonic-gate int
pthread_getconcurrency()23577257d1b4Sraf pthread_getconcurrency()
23587c478bd9Sstevel@tonic-gate {
23597c478bd9Sstevel@tonic-gate return (pthread_concurrency);
23607c478bd9Sstevel@tonic-gate }
23617c478bd9Sstevel@tonic-gate
23627c478bd9Sstevel@tonic-gate int
thr_setconcurrency(int new_level)23637257d1b4Sraf thr_setconcurrency(int new_level)
23647c478bd9Sstevel@tonic-gate {
23657c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
23667c478bd9Sstevel@tonic-gate
23677c478bd9Sstevel@tonic-gate if (new_level < 0)
23687c478bd9Sstevel@tonic-gate return (EINVAL);
23697c478bd9Sstevel@tonic-gate if (new_level > 65536) /* 65536 is totally arbitrary */
23707c478bd9Sstevel@tonic-gate return (EAGAIN);
23717c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
23727c478bd9Sstevel@tonic-gate if (new_level > thr_concurrency)
23737c478bd9Sstevel@tonic-gate thr_concurrency = new_level;
23747c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
23757c478bd9Sstevel@tonic-gate return (0);
23767c478bd9Sstevel@tonic-gate }
23777c478bd9Sstevel@tonic-gate
23787c478bd9Sstevel@tonic-gate int
pthread_setconcurrency(int new_level)23797257d1b4Sraf pthread_setconcurrency(int new_level)
23807c478bd9Sstevel@tonic-gate {
23817c478bd9Sstevel@tonic-gate if (new_level < 0)
23827c478bd9Sstevel@tonic-gate return (EINVAL);
23837c478bd9Sstevel@tonic-gate if (new_level > 65536) /* 65536 is totally arbitrary */
23847c478bd9Sstevel@tonic-gate return (EAGAIN);
23857c478bd9Sstevel@tonic-gate pthread_concurrency = new_level;
23867c478bd9Sstevel@tonic-gate return (0);
23877c478bd9Sstevel@tonic-gate }
23887c478bd9Sstevel@tonic-gate
23897c478bd9Sstevel@tonic-gate size_t
thr_min_stack(void)23907257d1b4Sraf thr_min_stack(void)
23917c478bd9Sstevel@tonic-gate {
23927c478bd9Sstevel@tonic-gate return (MINSTACK);
23937c478bd9Sstevel@tonic-gate }
23947c478bd9Sstevel@tonic-gate
23957c478bd9Sstevel@tonic-gate int
__nthreads(void)23967c478bd9Sstevel@tonic-gate __nthreads(void)
23977c478bd9Sstevel@tonic-gate {
23987c478bd9Sstevel@tonic-gate return (curthread->ul_uberdata->nthreads);
23997c478bd9Sstevel@tonic-gate }
24007c478bd9Sstevel@tonic-gate
2401ab618543SJohn Levon /* "/proc/self/lwp/%u/lwpname" w/o stdio */
2402ab618543SJohn Levon static void
lwpname_path(pthread_t tid,char * buf,size_t bufsize)2403ab618543SJohn Levon lwpname_path(pthread_t tid, char *buf, size_t bufsize)
2404ab618543SJohn Levon {
2405ab618543SJohn Levon (void) strlcpy(buf, "/proc/self/lwp/", bufsize);
2406ab618543SJohn Levon ultos((uint64_t)tid, 10, buf + strlen(buf));
2407ab618543SJohn Levon (void) strlcat(buf, "/lwpname", bufsize);
2408ab618543SJohn Levon }
2409ab618543SJohn Levon
2410ab618543SJohn Levon #pragma weak pthread_setname_np = thr_setname
2411ab618543SJohn Levon int
thr_setname(pthread_t tid,const char * name)2412ab618543SJohn Levon thr_setname(pthread_t tid, const char *name)
2413ab618543SJohn Levon {
2414ab618543SJohn Levon extern ssize_t __write(int, const void *, size_t);
2415ab618543SJohn Levon char path[PATH_MAX];
2416ab618543SJohn Levon int saved_errno;
2417ab618543SJohn Levon size_t len;
2418ab618543SJohn Levon ssize_t n;
2419ab618543SJohn Levon int fd;
2420ab618543SJohn Levon
2421ab618543SJohn Levon if (name == NULL)
2422ab618543SJohn Levon name = "";
2423ab618543SJohn Levon
2424ab618543SJohn Levon len = strlen(name) + 1;
2425ab618543SJohn Levon if (len > THREAD_NAME_MAX)
2426ab618543SJohn Levon return (ERANGE);
2427ab618543SJohn Levon
2428ab618543SJohn Levon lwpname_path(tid, path, sizeof (path));
2429ab618543SJohn Levon
2430ab618543SJohn Levon if ((fd = __open(path, O_WRONLY, 0)) < 0) {
2431ab618543SJohn Levon if (errno == ENOENT)
2432ab618543SJohn Levon errno = ESRCH;
2433ab618543SJohn Levon return (errno);
2434ab618543SJohn Levon }
2435ab618543SJohn Levon
2436ab618543SJohn Levon n = __write(fd, name, len);
2437ab618543SJohn Levon saved_errno = errno;
2438ab618543SJohn Levon (void) __close(fd);
2439ab618543SJohn Levon
2440ab618543SJohn Levon if (n < 0)
2441ab618543SJohn Levon return (saved_errno);
2442ab618543SJohn Levon if (n != len)
2443ab618543SJohn Levon return (EFAULT);
2444ab618543SJohn Levon return (0);
2445ab618543SJohn Levon }
2446ab618543SJohn Levon
2447ab618543SJohn Levon #pragma weak pthread_getname_np = thr_getname
2448ab618543SJohn Levon int
thr_getname(pthread_t tid,char * buf,size_t bufsize)2449ab618543SJohn Levon thr_getname(pthread_t tid, char *buf, size_t bufsize)
2450ab618543SJohn Levon {
2451ab618543SJohn Levon extern ssize_t __read(int, void *, size_t);
2452ab618543SJohn Levon char name[THREAD_NAME_MAX];
2453ab618543SJohn Levon char path[PATH_MAX];
2454ab618543SJohn Levon int saved_errno;
2455ab618543SJohn Levon ssize_t n;
2456ab618543SJohn Levon int fd;
2457ab618543SJohn Levon
2458ab618543SJohn Levon if (buf == NULL)
2459ab618543SJohn Levon return (EINVAL);
2460ab618543SJohn Levon
2461ab618543SJohn Levon lwpname_path(tid, path, sizeof (path));
2462ab618543SJohn Levon
2463ab618543SJohn Levon if ((fd = __open(path, O_RDONLY, 0)) < 0) {
2464ab618543SJohn Levon if (errno == ENOENT)
2465ab618543SJohn Levon errno = ESRCH;
2466ab618543SJohn Levon return (errno);
2467ab618543SJohn Levon }
2468ab618543SJohn Levon
2469ab618543SJohn Levon n = __read(fd, name, sizeof (name));
2470ab618543SJohn Levon saved_errno = errno;
2471ab618543SJohn Levon (void) __close(fd);
2472ab618543SJohn Levon
2473ab618543SJohn Levon if (n < 0)
2474ab618543SJohn Levon return (saved_errno);
2475ab618543SJohn Levon if (n != sizeof (name))
2476ab618543SJohn Levon return (EFAULT);
2477ab618543SJohn Levon if (strlcpy(buf, name, bufsize) >= bufsize)
2478ab618543SJohn Levon return (ERANGE);
2479ab618543SJohn Levon return (0);
2480ab618543SJohn Levon }
2481ab618543SJohn Levon
24827c478bd9Sstevel@tonic-gate /*
24837c478bd9Sstevel@tonic-gate * XXX
24847c478bd9Sstevel@tonic-gate * The remainder of this file implements the private interfaces to java for
24857c478bd9Sstevel@tonic-gate * garbage collection. It is no longer used, at least by java 1.2.
24867c478bd9Sstevel@tonic-gate * It can all go away once all old JVMs have disappeared.
24877c478bd9Sstevel@tonic-gate */
24887c478bd9Sstevel@tonic-gate
24897c478bd9Sstevel@tonic-gate int suspendingallmutators; /* when non-zero, suspending all mutators. */
24907c478bd9Sstevel@tonic-gate int suspendedallmutators; /* when non-zero, all mutators suspended. */
24917c478bd9Sstevel@tonic-gate int mutatorsbarrier; /* when non-zero, mutators barrier imposed. */
24927c478bd9Sstevel@tonic-gate mutex_t mutatorslock = DEFAULTMUTEX; /* used to enforce mutators barrier. */
24937c478bd9Sstevel@tonic-gate cond_t mutatorscv = DEFAULTCV; /* where non-mutators sleep. */
24947c478bd9Sstevel@tonic-gate
24957c478bd9Sstevel@tonic-gate /*
24967c478bd9Sstevel@tonic-gate * Get the available register state for the target thread.
24977c478bd9Sstevel@tonic-gate * Return non-volatile registers: TRS_NONVOLATILE
24987c478bd9Sstevel@tonic-gate */
24997257d1b4Sraf #pragma weak _thr_getstate = thr_getstate
25007c478bd9Sstevel@tonic-gate int
thr_getstate(thread_t tid,int * flag,lwpid_t * lwp,stack_t * ss,gregset_t rs)25017257d1b4Sraf thr_getstate(thread_t tid, int *flag, lwpid_t *lwp, stack_t *ss, gregset_t rs)
25027c478bd9Sstevel@tonic-gate {
25037c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
25047c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
25057c478bd9Sstevel@tonic-gate ulwp_t **ulwpp;
25067c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
25077c478bd9Sstevel@tonic-gate int error = 0;
25087c478bd9Sstevel@tonic-gate int trs_flag = TRS_LWPID;
25097c478bd9Sstevel@tonic-gate
25107c478bd9Sstevel@tonic-gate if (tid == 0 || self->ul_lwpid == tid) {
25117c478bd9Sstevel@tonic-gate ulwp = self;
25127c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp);
25137c478bd9Sstevel@tonic-gate } else if ((ulwpp = find_lwpp(tid)) != NULL) {
25147c478bd9Sstevel@tonic-gate ulwp = *ulwpp;
25157c478bd9Sstevel@tonic-gate } else {
25167c478bd9Sstevel@tonic-gate if (flag)
25177c478bd9Sstevel@tonic-gate *flag = TRS_INVALID;
25187c478bd9Sstevel@tonic-gate return (ESRCH);
25197c478bd9Sstevel@tonic-gate }
25207c478bd9Sstevel@tonic-gate
25217c478bd9Sstevel@tonic-gate if (ulwp->ul_dead) {
25227c478bd9Sstevel@tonic-gate trs_flag = TRS_INVALID;
25237c478bd9Sstevel@tonic-gate } else if (!ulwp->ul_stop && !suspendedallmutators) {
25247c478bd9Sstevel@tonic-gate error = EINVAL;
25257c478bd9Sstevel@tonic-gate trs_flag = TRS_INVALID;
25267c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop) {
25277c478bd9Sstevel@tonic-gate trs_flag = TRS_NONVOLATILE;
25287c478bd9Sstevel@tonic-gate getgregs(ulwp, rs);
25297c478bd9Sstevel@tonic-gate }
25307c478bd9Sstevel@tonic-gate
25317c478bd9Sstevel@tonic-gate if (flag)
25327c478bd9Sstevel@tonic-gate *flag = trs_flag;
25337c478bd9Sstevel@tonic-gate if (lwp)
25347c478bd9Sstevel@tonic-gate *lwp = tid;
25357c478bd9Sstevel@tonic-gate if (ss != NULL)
25367c478bd9Sstevel@tonic-gate (void) _thrp_stksegment(ulwp, ss);
25377c478bd9Sstevel@tonic-gate
25387c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
25397c478bd9Sstevel@tonic-gate return (error);
25407c478bd9Sstevel@tonic-gate }
25417c478bd9Sstevel@tonic-gate
25427c478bd9Sstevel@tonic-gate /*
25437c478bd9Sstevel@tonic-gate * Set the appropriate register state for the target thread.
25447c478bd9Sstevel@tonic-gate * This is not used by java. It exists solely for the MSTC test suite.
25457c478bd9Sstevel@tonic-gate */
25467257d1b4Sraf #pragma weak _thr_setstate = thr_setstate
25477c478bd9Sstevel@tonic-gate int
thr_setstate(thread_t tid,int flag,gregset_t rs)25487257d1b4Sraf thr_setstate(thread_t tid, int flag, gregset_t rs)
25497c478bd9Sstevel@tonic-gate {
25507c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
25517c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
25527c478bd9Sstevel@tonic-gate int error = 0;
25537c478bd9Sstevel@tonic-gate
25547c478bd9Sstevel@tonic-gate if ((ulwp = find_lwp(tid)) == NULL)
25557c478bd9Sstevel@tonic-gate return (ESRCH);
25567c478bd9Sstevel@tonic-gate
25577c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop && !suspendedallmutators)
25587c478bd9Sstevel@tonic-gate error = EINVAL;
25597c478bd9Sstevel@tonic-gate else if (rs != NULL) {
25607c478bd9Sstevel@tonic-gate switch (flag) {
25617c478bd9Sstevel@tonic-gate case TRS_NONVOLATILE:
25627c478bd9Sstevel@tonic-gate /* do /proc stuff here? */
25637c478bd9Sstevel@tonic-gate if (ulwp->ul_stop)
25647c478bd9Sstevel@tonic-gate setgregs(ulwp, rs);
25657c478bd9Sstevel@tonic-gate else
25667c478bd9Sstevel@tonic-gate error = EINVAL;
25677c478bd9Sstevel@tonic-gate break;
25687c478bd9Sstevel@tonic-gate case TRS_LWPID: /* do /proc stuff here? */
25697c478bd9Sstevel@tonic-gate default:
25707c478bd9Sstevel@tonic-gate error = EINVAL;
25717c478bd9Sstevel@tonic-gate break;
25727c478bd9Sstevel@tonic-gate }
25737c478bd9Sstevel@tonic-gate }
25747c478bd9Sstevel@tonic-gate
25757c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
25767c478bd9Sstevel@tonic-gate return (error);
25777c478bd9Sstevel@tonic-gate }
25787c478bd9Sstevel@tonic-gate
25797c478bd9Sstevel@tonic-gate int
getlwpstatus(thread_t tid,struct lwpstatus * sp)25807c478bd9Sstevel@tonic-gate getlwpstatus(thread_t tid, struct lwpstatus *sp)
25817c478bd9Sstevel@tonic-gate {
2582a574db85Sraf extern ssize_t __pread(int, void *, size_t, off_t);
25837c478bd9Sstevel@tonic-gate char buf[100];
25847c478bd9Sstevel@tonic-gate int fd;
25857c478bd9Sstevel@tonic-gate
25867c478bd9Sstevel@tonic-gate /* "/proc/self/lwp/%u/lwpstatus" w/o stdio */
25877c478bd9Sstevel@tonic-gate (void) strcpy(buf, "/proc/self/lwp/");
25887c478bd9Sstevel@tonic-gate ultos((uint64_t)tid, 10, buf + strlen(buf));
25897c478bd9Sstevel@tonic-gate (void) strcat(buf, "/lwpstatus");
25908cd45542Sraf if ((fd = __open(buf, O_RDONLY, 0)) >= 0) {
2591a574db85Sraf while (__pread(fd, sp, sizeof (*sp), 0) == sizeof (*sp)) {
25927c478bd9Sstevel@tonic-gate if (sp->pr_flags & PR_STOPPED) {
25938cd45542Sraf (void) __close(fd);
25947c478bd9Sstevel@tonic-gate return (0);
25957c478bd9Sstevel@tonic-gate }
259648bbca81SDaniel Hoffman yield(); /* give it a chance to stop */
25977c478bd9Sstevel@tonic-gate }
25988cd45542Sraf (void) __close(fd);
25997c478bd9Sstevel@tonic-gate }
26007c478bd9Sstevel@tonic-gate return (-1);
26017c478bd9Sstevel@tonic-gate }
26027c478bd9Sstevel@tonic-gate
26037c478bd9Sstevel@tonic-gate int
putlwpregs(thread_t tid,prgregset_t prp)26047c478bd9Sstevel@tonic-gate putlwpregs(thread_t tid, prgregset_t prp)
26057c478bd9Sstevel@tonic-gate {
2606a574db85Sraf extern ssize_t __writev(int, const struct iovec *, int);
26077c478bd9Sstevel@tonic-gate char buf[100];
26087c478bd9Sstevel@tonic-gate int fd;
26097c478bd9Sstevel@tonic-gate long dstop_sreg[2];
26107c478bd9Sstevel@tonic-gate long run_null[2];
26117c478bd9Sstevel@tonic-gate iovec_t iov[3];
26127c478bd9Sstevel@tonic-gate
26137c478bd9Sstevel@tonic-gate /* "/proc/self/lwp/%u/lwpctl" w/o stdio */
26147c478bd9Sstevel@tonic-gate (void) strcpy(buf, "/proc/self/lwp/");
26157c478bd9Sstevel@tonic-gate ultos((uint64_t)tid, 10, buf + strlen(buf));
26167c478bd9Sstevel@tonic-gate (void) strcat(buf, "/lwpctl");
26178cd45542Sraf if ((fd = __open(buf, O_WRONLY, 0)) >= 0) {
26187c478bd9Sstevel@tonic-gate dstop_sreg[0] = PCDSTOP; /* direct it to stop */
26197c478bd9Sstevel@tonic-gate dstop_sreg[1] = PCSREG; /* set the registers */
26207c478bd9Sstevel@tonic-gate iov[0].iov_base = (caddr_t)dstop_sreg;
26217c478bd9Sstevel@tonic-gate iov[0].iov_len = sizeof (dstop_sreg);
26227c478bd9Sstevel@tonic-gate iov[1].iov_base = (caddr_t)prp; /* from the register set */
26237c478bd9Sstevel@tonic-gate iov[1].iov_len = sizeof (prgregset_t);
26247c478bd9Sstevel@tonic-gate run_null[0] = PCRUN; /* make it runnable again */
26257c478bd9Sstevel@tonic-gate run_null[1] = 0;
26267c478bd9Sstevel@tonic-gate iov[2].iov_base = (caddr_t)run_null;
26277c478bd9Sstevel@tonic-gate iov[2].iov_len = sizeof (run_null);
2628a574db85Sraf if (__writev(fd, iov, 3) >= 0) {
26298cd45542Sraf (void) __close(fd);
26307c478bd9Sstevel@tonic-gate return (0);
26317c478bd9Sstevel@tonic-gate }
26328cd45542Sraf (void) __close(fd);
26337c478bd9Sstevel@tonic-gate }
26347c478bd9Sstevel@tonic-gate return (-1);
26357c478bd9Sstevel@tonic-gate }
26367c478bd9Sstevel@tonic-gate
26377c478bd9Sstevel@tonic-gate static ulong_t
gettsp_slow(thread_t tid)26387c478bd9Sstevel@tonic-gate gettsp_slow(thread_t tid)
26397c478bd9Sstevel@tonic-gate {
26407c478bd9Sstevel@tonic-gate char buf[100];
26417c478bd9Sstevel@tonic-gate struct lwpstatus status;
26427c478bd9Sstevel@tonic-gate
26437c478bd9Sstevel@tonic-gate if (getlwpstatus(tid, &status) != 0) {
26447c478bd9Sstevel@tonic-gate /* "__gettsp(%u): can't read lwpstatus" w/o stdio */
26457c478bd9Sstevel@tonic-gate (void) strcpy(buf, "__gettsp(");
26467c478bd9Sstevel@tonic-gate ultos((uint64_t)tid, 10, buf + strlen(buf));
26477c478bd9Sstevel@tonic-gate (void) strcat(buf, "): can't read lwpstatus");
26487c478bd9Sstevel@tonic-gate thr_panic(buf);
26497c478bd9Sstevel@tonic-gate }
26507c478bd9Sstevel@tonic-gate return (status.pr_reg[R_SP]);
26517c478bd9Sstevel@tonic-gate }
26527c478bd9Sstevel@tonic-gate
26537c478bd9Sstevel@tonic-gate ulong_t
__gettsp(thread_t tid)26547c478bd9Sstevel@tonic-gate __gettsp(thread_t tid)
26557c478bd9Sstevel@tonic-gate {
26567c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
26577c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
26587c478bd9Sstevel@tonic-gate ulong_t result;
26597c478bd9Sstevel@tonic-gate
26607c478bd9Sstevel@tonic-gate if ((ulwp = find_lwp(tid)) == NULL)
26617c478bd9Sstevel@tonic-gate return (0);
26627c478bd9Sstevel@tonic-gate
26637c478bd9Sstevel@tonic-gate if (ulwp->ul_stop && (result = ulwp->ul_sp) != 0) {
26647c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
26657c478bd9Sstevel@tonic-gate return (result);
26667c478bd9Sstevel@tonic-gate }
26677c478bd9Sstevel@tonic-gate
26687c478bd9Sstevel@tonic-gate result = gettsp_slow(tid);
26697c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
26707c478bd9Sstevel@tonic-gate return (result);
26717c478bd9Sstevel@tonic-gate }
26727c478bd9Sstevel@tonic-gate
26737c478bd9Sstevel@tonic-gate /*
26747c478bd9Sstevel@tonic-gate * This tells java stack walkers how to find the ucontext
26757c478bd9Sstevel@tonic-gate * structure passed to signal handlers.
26767c478bd9Sstevel@tonic-gate */
26777257d1b4Sraf #pragma weak _thr_sighndlrinfo = thr_sighndlrinfo
26787c478bd9Sstevel@tonic-gate void
thr_sighndlrinfo(void (** func)(),int * funcsize)26797257d1b4Sraf thr_sighndlrinfo(void (**func)(), int *funcsize)
26807c478bd9Sstevel@tonic-gate {
26817c478bd9Sstevel@tonic-gate *func = &__sighndlr;
26827c478bd9Sstevel@tonic-gate *funcsize = (char *)&__sighndlrend - (char *)&__sighndlr;
26837c478bd9Sstevel@tonic-gate }
26847c478bd9Sstevel@tonic-gate
26857c478bd9Sstevel@tonic-gate /*
26867c478bd9Sstevel@tonic-gate * Mark a thread a mutator or reset a mutator to being a default,
26877c478bd9Sstevel@tonic-gate * non-mutator thread.
26887c478bd9Sstevel@tonic-gate */
26897257d1b4Sraf #pragma weak _thr_setmutator = thr_setmutator
26907c478bd9Sstevel@tonic-gate int
thr_setmutator(thread_t tid,int enabled)26917257d1b4Sraf thr_setmutator(thread_t tid, int enabled)
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 error;
2697a574db85Sraf int cancel_state;
26987c478bd9Sstevel@tonic-gate
2699a574db85Sraf enabled = enabled? 1 : 0;
27007c478bd9Sstevel@tonic-gate top:
27017c478bd9Sstevel@tonic-gate if (tid == 0) {
27027c478bd9Sstevel@tonic-gate ulwp = self;
27037c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp);
27047c478bd9Sstevel@tonic-gate } else if ((ulwp = find_lwp(tid)) == NULL) {
27057c478bd9Sstevel@tonic-gate return (ESRCH);
27067c478bd9Sstevel@tonic-gate }
27077c478bd9Sstevel@tonic-gate
27087c478bd9Sstevel@tonic-gate /*
27097c478bd9Sstevel@tonic-gate * The target thread should be the caller itself or a suspended thread.
27107c478bd9Sstevel@tonic-gate * This prevents the target from also changing its ul_mutator field.
27117c478bd9Sstevel@tonic-gate */
27127c478bd9Sstevel@tonic-gate error = 0;
27137c478bd9Sstevel@tonic-gate if (ulwp != self && !ulwp->ul_stop && enabled)
27147c478bd9Sstevel@tonic-gate error = EINVAL;
27157c478bd9Sstevel@tonic-gate else if (ulwp->ul_mutator != enabled) {
27167c478bd9Sstevel@tonic-gate lmutex_lock(&mutatorslock);
27177c478bd9Sstevel@tonic-gate if (mutatorsbarrier) {
27187c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
27197257d1b4Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
2720a574db85Sraf &cancel_state);
27217c478bd9Sstevel@tonic-gate while (mutatorsbarrier)
27227257d1b4Sraf (void) cond_wait(&mutatorscv, &mutatorslock);
27237257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL);
27247c478bd9Sstevel@tonic-gate lmutex_unlock(&mutatorslock);
27257c478bd9Sstevel@tonic-gate goto top;
27267c478bd9Sstevel@tonic-gate }
27277c478bd9Sstevel@tonic-gate ulwp->ul_mutator = enabled;
27287c478bd9Sstevel@tonic-gate lmutex_unlock(&mutatorslock);
27297c478bd9Sstevel@tonic-gate }
27307c478bd9Sstevel@tonic-gate
27317c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
27327c478bd9Sstevel@tonic-gate return (error);
27337c478bd9Sstevel@tonic-gate }
27347c478bd9Sstevel@tonic-gate
27357c478bd9Sstevel@tonic-gate /*
27367c478bd9Sstevel@tonic-gate * Establish a barrier against new mutators. Any non-mutator trying
27377c478bd9Sstevel@tonic-gate * to become a mutator is suspended until the barrier is removed.
27387c478bd9Sstevel@tonic-gate */
27397257d1b4Sraf #pragma weak _thr_mutators_barrier = thr_mutators_barrier
27407c478bd9Sstevel@tonic-gate void
thr_mutators_barrier(int enabled)27417257d1b4Sraf thr_mutators_barrier(int enabled)
27427c478bd9Sstevel@tonic-gate {
27437c478bd9Sstevel@tonic-gate int oldvalue;
2744a574db85Sraf int cancel_state;
27457c478bd9Sstevel@tonic-gate
27467c478bd9Sstevel@tonic-gate lmutex_lock(&mutatorslock);
27477c478bd9Sstevel@tonic-gate
27487c478bd9Sstevel@tonic-gate /*
27497c478bd9Sstevel@tonic-gate * Wait if trying to set the barrier while it is already set.
27507c478bd9Sstevel@tonic-gate */
27517257d1b4Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
27527c478bd9Sstevel@tonic-gate while (mutatorsbarrier && enabled)
27537257d1b4Sraf (void) cond_wait(&mutatorscv, &mutatorslock);
27547257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL);
27557c478bd9Sstevel@tonic-gate
27567c478bd9Sstevel@tonic-gate oldvalue = mutatorsbarrier;
27577c478bd9Sstevel@tonic-gate mutatorsbarrier = enabled;
27587c478bd9Sstevel@tonic-gate /*
27597c478bd9Sstevel@tonic-gate * Wakeup any blocked non-mutators when barrier is removed.
27607c478bd9Sstevel@tonic-gate */
27617c478bd9Sstevel@tonic-gate if (oldvalue && !enabled)
27627257d1b4Sraf (void) cond_broadcast(&mutatorscv);
27637c478bd9Sstevel@tonic-gate lmutex_unlock(&mutatorslock);
27647c478bd9Sstevel@tonic-gate }
27657c478bd9Sstevel@tonic-gate
27667c478bd9Sstevel@tonic-gate /*
27677c478bd9Sstevel@tonic-gate * Suspend the set of all mutators except for the caller. The list
27687c478bd9Sstevel@tonic-gate * of actively running threads is searched and only the mutators
27697c478bd9Sstevel@tonic-gate * in this list are suspended. Actively running non-mutators remain
27707c478bd9Sstevel@tonic-gate * running. Any other thread is suspended.
27717c478bd9Sstevel@tonic-gate */
27727257d1b4Sraf #pragma weak _thr_suspend_allmutators = thr_suspend_allmutators
27737c478bd9Sstevel@tonic-gate int
thr_suspend_allmutators(void)27747257d1b4Sraf thr_suspend_allmutators(void)
27757c478bd9Sstevel@tonic-gate {
27767c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
27777c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
27787c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
27797c478bd9Sstevel@tonic-gate int link_dropped;
27807c478bd9Sstevel@tonic-gate
27817c478bd9Sstevel@tonic-gate /*
278236319254Sraf * We single-thread the entire thread suspend/continue mechanism.
27837c478bd9Sstevel@tonic-gate */
27842e145884Sraf fork_lock_enter();
278536319254Sraf
27867c478bd9Sstevel@tonic-gate top:
27877c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
27887c478bd9Sstevel@tonic-gate
27897c478bd9Sstevel@tonic-gate if (suspendingallmutators || suspendedallmutators) {
27907c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
27917c478bd9Sstevel@tonic-gate fork_lock_exit();
27927c478bd9Sstevel@tonic-gate return (EINVAL);
27937c478bd9Sstevel@tonic-gate }
27947c478bd9Sstevel@tonic-gate suspendingallmutators = 1;
27957c478bd9Sstevel@tonic-gate
27967c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
27977c478bd9Sstevel@tonic-gate ulwp_lock(ulwp, udp);
27987c478bd9Sstevel@tonic-gate if (!ulwp->ul_mutator) {
27997c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
28007c478bd9Sstevel@tonic-gate } else if (ulwp->ul_stop) { /* already stopped */
28017c478bd9Sstevel@tonic-gate ulwp->ul_stop |= TSTP_MUTATOR;
28027c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
28037c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
28047c478bd9Sstevel@tonic-gate } else {
28057c478bd9Sstevel@tonic-gate /*
28067c478bd9Sstevel@tonic-gate * Move the stopped lwp out of a critical section.
28077c478bd9Sstevel@tonic-gate */
28087c478bd9Sstevel@tonic-gate if (safe_suspend(ulwp, TSTP_MUTATOR, &link_dropped) ||
28097c478bd9Sstevel@tonic-gate link_dropped) {
28107c478bd9Sstevel@tonic-gate suspendingallmutators = 0;
28117c478bd9Sstevel@tonic-gate goto top;
28127c478bd9Sstevel@tonic-gate }
28137c478bd9Sstevel@tonic-gate }
28147c478bd9Sstevel@tonic-gate }
28157c478bd9Sstevel@tonic-gate
28167c478bd9Sstevel@tonic-gate suspendedallmutators = 1;
28177c478bd9Sstevel@tonic-gate suspendingallmutators = 0;
28187c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
28197c478bd9Sstevel@tonic-gate fork_lock_exit();
28207c478bd9Sstevel@tonic-gate return (0);
28217c478bd9Sstevel@tonic-gate }
28227c478bd9Sstevel@tonic-gate
28237c478bd9Sstevel@tonic-gate /*
28247c478bd9Sstevel@tonic-gate * Suspend the target mutator. The caller is permitted to suspend
28257c478bd9Sstevel@tonic-gate * itself. If a mutator barrier is enabled, the caller will suspend
28267c478bd9Sstevel@tonic-gate * itself as though it had been suspended by thr_suspend_allmutators().
28277c478bd9Sstevel@tonic-gate * When the barrier is removed, this thread will be resumed. Any
28287c478bd9Sstevel@tonic-gate * suspended mutator, whether suspended by thr_suspend_mutator(), or by
28297c478bd9Sstevel@tonic-gate * thr_suspend_allmutators(), can be resumed by thr_continue_mutator().
28307c478bd9Sstevel@tonic-gate */
28317257d1b4Sraf #pragma weak _thr_suspend_mutator = thr_suspend_mutator
28327c478bd9Sstevel@tonic-gate int
thr_suspend_mutator(thread_t tid)28337257d1b4Sraf thr_suspend_mutator(thread_t tid)
28347c478bd9Sstevel@tonic-gate {
28357c478bd9Sstevel@tonic-gate if (tid == 0)
28367c478bd9Sstevel@tonic-gate tid = curthread->ul_lwpid;
28377c478bd9Sstevel@tonic-gate return (_thrp_suspend(tid, TSTP_MUTATOR));
28387c478bd9Sstevel@tonic-gate }
28397c478bd9Sstevel@tonic-gate
28407c478bd9Sstevel@tonic-gate /*
28417c478bd9Sstevel@tonic-gate * Resume the set of all suspended mutators.
28427c478bd9Sstevel@tonic-gate */
28437257d1b4Sraf #pragma weak _thr_continue_allmutators = thr_continue_allmutators
28447c478bd9Sstevel@tonic-gate int
thr_continue_allmutators()28457257d1b4Sraf thr_continue_allmutators()
28467c478bd9Sstevel@tonic-gate {
28477c478bd9Sstevel@tonic-gate ulwp_t *self = curthread;
28487c478bd9Sstevel@tonic-gate uberdata_t *udp = self->ul_uberdata;
28497c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
285036319254Sraf
285136319254Sraf /*
285236319254Sraf * We single-thread the entire thread suspend/continue mechanism.
285336319254Sraf */
28542e145884Sraf fork_lock_enter();
28557c478bd9Sstevel@tonic-gate
28567c478bd9Sstevel@tonic-gate lmutex_lock(&udp->link_lock);
28577c478bd9Sstevel@tonic-gate if (!suspendedallmutators) {
28587c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
285936319254Sraf fork_lock_exit();
28607c478bd9Sstevel@tonic-gate return (EINVAL);
28617c478bd9Sstevel@tonic-gate }
28627c478bd9Sstevel@tonic-gate suspendedallmutators = 0;
28637c478bd9Sstevel@tonic-gate
28647c478bd9Sstevel@tonic-gate for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
28657c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp);
28667c478bd9Sstevel@tonic-gate lmutex_lock(mp);
28677c478bd9Sstevel@tonic-gate if (ulwp->ul_stop & TSTP_MUTATOR) {
28687c478bd9Sstevel@tonic-gate ulwp->ul_stop &= ~TSTP_MUTATOR;
28697c478bd9Sstevel@tonic-gate ulwp_broadcast(ulwp);
28707c478bd9Sstevel@tonic-gate if (!ulwp->ul_stop)
28717c478bd9Sstevel@tonic-gate force_continue(ulwp);
28727c478bd9Sstevel@tonic-gate }
28737c478bd9Sstevel@tonic-gate lmutex_unlock(mp);
28747c478bd9Sstevel@tonic-gate }
28757c478bd9Sstevel@tonic-gate
28767c478bd9Sstevel@tonic-gate lmutex_unlock(&udp->link_lock);
287736319254Sraf fork_lock_exit();
28787c478bd9Sstevel@tonic-gate return (0);
28797c478bd9Sstevel@tonic-gate }
28807c478bd9Sstevel@tonic-gate
28817c478bd9Sstevel@tonic-gate /*
28827c478bd9Sstevel@tonic-gate * Resume a suspended mutator.
28837c478bd9Sstevel@tonic-gate */
28847257d1b4Sraf #pragma weak _thr_continue_mutator = thr_continue_mutator
28857c478bd9Sstevel@tonic-gate int
thr_continue_mutator(thread_t tid)28867257d1b4Sraf thr_continue_mutator(thread_t tid)
28877c478bd9Sstevel@tonic-gate {
28887c478bd9Sstevel@tonic-gate return (_thrp_continue(tid, TSTP_MUTATOR));
28897c478bd9Sstevel@tonic-gate }
28907c478bd9Sstevel@tonic-gate
28917257d1b4Sraf #pragma weak _thr_wait_mutator = thr_wait_mutator
28927c478bd9Sstevel@tonic-gate int
thr_wait_mutator(thread_t tid,int dontwait)28937257d1b4Sraf thr_wait_mutator(thread_t tid, int dontwait)
28947c478bd9Sstevel@tonic-gate {
28957c478bd9Sstevel@tonic-gate uberdata_t *udp = curthread->ul_uberdata;
28967c478bd9Sstevel@tonic-gate ulwp_t *ulwp;
2897a574db85Sraf int cancel_state;
28987c478bd9Sstevel@tonic-gate int error = 0;
28997c478bd9Sstevel@tonic-gate
29007257d1b4Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
29017c478bd9Sstevel@tonic-gate top:
2902a574db85Sraf if ((ulwp = find_lwp(tid)) == NULL) {
29037257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL);
29047c478bd9Sstevel@tonic-gate return (ESRCH);
2905a574db85Sraf }
29067c478bd9Sstevel@tonic-gate
29077c478bd9Sstevel@tonic-gate if (!ulwp->ul_mutator)
29087c478bd9Sstevel@tonic-gate error = EINVAL;
29097c478bd9Sstevel@tonic-gate else if (dontwait) {
29107c478bd9Sstevel@tonic-gate if (!(ulwp->ul_stop & TSTP_MUTATOR))
29117c478bd9Sstevel@tonic-gate error = EWOULDBLOCK;
29127c478bd9Sstevel@tonic-gate } else if (!(ulwp->ul_stop & TSTP_MUTATOR)) {
29137c478bd9Sstevel@tonic-gate cond_t *cvp = ulwp_condvar(ulwp, udp);
29147c478bd9Sstevel@tonic-gate mutex_t *mp = ulwp_mutex(ulwp, udp);
29157c478bd9Sstevel@tonic-gate
29167257d1b4Sraf (void) cond_wait(cvp, mp);
29177c478bd9Sstevel@tonic-gate (void) lmutex_unlock(mp);
29187c478bd9Sstevel@tonic-gate goto top;
29197c478bd9Sstevel@tonic-gate }
29207c478bd9Sstevel@tonic-gate
29217c478bd9Sstevel@tonic-gate ulwp_unlock(ulwp, udp);
29227257d1b4Sraf (void) pthread_setcancelstate(cancel_state, NULL);
29237c478bd9Sstevel@tonic-gate return (error);
29247c478bd9Sstevel@tonic-gate }
29257c478bd9Sstevel@tonic-gate
29267c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT begin */
29277c478bd9Sstevel@tonic-gate
29287c478bd9Sstevel@tonic-gate void
thr_probe_setup(void * data)29297c478bd9Sstevel@tonic-gate thr_probe_setup(void *data)
29307c478bd9Sstevel@tonic-gate {
29317c478bd9Sstevel@tonic-gate curthread->ul_tpdp = data;
29327c478bd9Sstevel@tonic-gate }
29337c478bd9Sstevel@tonic-gate
29347c478bd9Sstevel@tonic-gate static void *
_thread_probe_getfunc()29357c478bd9Sstevel@tonic-gate _thread_probe_getfunc()
29367c478bd9Sstevel@tonic-gate {
29377c478bd9Sstevel@tonic-gate return (curthread->ul_tpdp);
29387c478bd9Sstevel@tonic-gate }
29397c478bd9Sstevel@tonic-gate
29407c478bd9Sstevel@tonic-gate void * (*thr_probe_getfunc_addr)(void) = _thread_probe_getfunc;
29417c478bd9Sstevel@tonic-gate
29427c478bd9Sstevel@tonic-gate /* ARGSUSED */
29437c478bd9Sstevel@tonic-gate void
_resume(ulwp_t * ulwp,caddr_t sp,int dontsave)29447c478bd9Sstevel@tonic-gate _resume(ulwp_t *ulwp, caddr_t sp, int dontsave)
29457c478bd9Sstevel@tonic-gate {
29467c478bd9Sstevel@tonic-gate /* never called */
29477c478bd9Sstevel@tonic-gate }
29487c478bd9Sstevel@tonic-gate
29497c478bd9Sstevel@tonic-gate /* ARGSUSED */
29507c478bd9Sstevel@tonic-gate void
_resume_ret(ulwp_t * oldlwp)29517c478bd9Sstevel@tonic-gate _resume_ret(ulwp_t *oldlwp)
29527c478bd9Sstevel@tonic-gate {
29537c478bd9Sstevel@tonic-gate /* never called */
29547c478bd9Sstevel@tonic-gate }
29557c478bd9Sstevel@tonic-gate
29567c478bd9Sstevel@tonic-gate /* PROBE_SUPPORT end */
2957