xref: /onnv-gate/usr/src/lib/libc/port/threads/thr.c (revision 12795:75bba6ac5f93)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51672Sraf  * Common Development and Distribution License (the "License").
61672Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211111Sraf 
220Sstevel@tonic-gate /*
23*12795SRoger.Faulkner@Oracle.COM  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include "lint.h"
270Sstevel@tonic-gate #include "thr_uberdata.h"
285891Sraf #include <pthread.h>
290Sstevel@tonic-gate #include <procfs.h>
300Sstevel@tonic-gate #include <sys/uio.h>
310Sstevel@tonic-gate #include <ctype.h>
325891Sraf #include "libc.h"
330Sstevel@tonic-gate 
346812Sraf /*
356812Sraf  * These symbols should not be exported from libc, but
366812Sraf  * /lib/libm.so.2 references _thr_main.  libm needs to be fixed.
376812Sraf  * Also, some older versions of the Studio compiler/debugger
386812Sraf  * components reference them.  These need to be fixed, too.
396812Sraf  */
406812Sraf #pragma weak _thr_main = thr_main
416812Sraf #pragma weak _thr_create = thr_create
426812Sraf #pragma weak _thr_join = thr_join
436812Sraf #pragma weak _thr_self = thr_self
446812Sraf 
450Sstevel@tonic-gate #undef errno
460Sstevel@tonic-gate extern int errno;
470Sstevel@tonic-gate 
481672Sraf /*
491672Sraf  * Between Solaris 2.5 and Solaris 9, __threaded was used to indicate
501672Sraf  * "we are linked with libthread".  The Sun Workshop 6 update 1 compilation
511672Sraf  * system used it illegally (it is a consolidation private symbol).
521672Sraf  * To accommodate this and possibly other abusers of the symbol,
531672Sraf  * we make it always equal to 1 now that libthread has been folded
541672Sraf  * into libc.  The new __libc_threaded symbol is used to indicate
551672Sraf  * the new meaning, "more than one thread exists".
561672Sraf  */
571672Sraf int __threaded = 1;		/* always equal to 1 */
581672Sraf int __libc_threaded = 0;	/* zero until first thr_create() */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * thr_concurrency and pthread_concurrency are not used by the library.
620Sstevel@tonic-gate  * They exist solely to hold and return the values set by calls to
630Sstevel@tonic-gate  * thr_setconcurrency() and pthread_setconcurrency().
640Sstevel@tonic-gate  * Because thr_concurrency is affected by the THR_NEW_LWP flag
650Sstevel@tonic-gate  * to thr_create(), thr_concurrency is protected by link_lock.
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate static	int	thr_concurrency = 1;
680Sstevel@tonic-gate static	int	pthread_concurrency;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #define	HASHTBLSZ	1024	/* must be a power of two */
710Sstevel@tonic-gate #define	TIDHASH(tid, udp)	(tid & (udp)->hash_mask)
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* initial allocation, just enough for one lwp */
740Sstevel@tonic-gate #pragma align 64(init_hash_table)
750Sstevel@tonic-gate thr_hash_table_t init_hash_table[1] = {
760Sstevel@tonic-gate 	{ DEFAULTMUTEX, DEFAULTCV, NULL },
770Sstevel@tonic-gate };
780Sstevel@tonic-gate 
790Sstevel@tonic-gate extern const Lc_interface rtld_funcs[];
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * The weak version is known to libc_db and mdb.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate #pragma weak _uberdata = __uberdata
850Sstevel@tonic-gate uberdata_t __uberdata = {
863512Sraf 	{ DEFAULTMUTEX, NULL, 0 },	/* link_lock */
876515Sraf 	{ RECURSIVEMUTEX, NULL, 0 },	/* ld_lock */
883512Sraf 	{ RECURSIVEMUTEX, NULL, 0 },	/* fork_lock */
894843Sraf 	{ RECURSIVEMUTEX, NULL, 0 },	/* atfork_lock */
905002Sraf 	{ RECURSIVEMUTEX, NULL, 0 },	/* callout_lock */
913512Sraf 	{ DEFAULTMUTEX, NULL, 0 },	/* tdb_hash_lock */
923512Sraf 	{ 0, },				/* tdb_hash_lock_stats */
933512Sraf 	{ { 0 }, },			/* siguaction[NSIG] */
940Sstevel@tonic-gate 	{{ DEFAULTMUTEX, NULL, 0 },		/* bucket[NBUCKETS] */
950Sstevel@tonic-gate 	{ DEFAULTMUTEX, NULL, 0 },
960Sstevel@tonic-gate 	{ DEFAULTMUTEX, NULL, 0 },
970Sstevel@tonic-gate 	{ DEFAULTMUTEX, NULL, 0 },
980Sstevel@tonic-gate 	{ DEFAULTMUTEX, NULL, 0 },
990Sstevel@tonic-gate 	{ DEFAULTMUTEX, NULL, 0 },
1000Sstevel@tonic-gate 	{ DEFAULTMUTEX, NULL, 0 },
1010Sstevel@tonic-gate 	{ DEFAULTMUTEX, NULL, 0 },
1020Sstevel@tonic-gate 	{ DEFAULTMUTEX, NULL, 0 },
1030Sstevel@tonic-gate 	{ DEFAULTMUTEX, NULL, 0 }},
1040Sstevel@tonic-gate 	{ RECURSIVEMUTEX, NULL, NULL },		/* atexit_root */
1050Sstevel@tonic-gate 	{ DEFAULTMUTEX, 0, 0, NULL },		/* tsd_metadata */
1060Sstevel@tonic-gate 	{ DEFAULTMUTEX, {0, 0}, {0, 0} },	/* tls_metadata */
1070Sstevel@tonic-gate 	0,			/* primary_map */
1080Sstevel@tonic-gate 	0,			/* bucket_init */
1090Sstevel@tonic-gate 	0,			/* pad[0] */
1100Sstevel@tonic-gate 	0,			/* pad[1] */
1110Sstevel@tonic-gate 	{ 0 },			/* uberflags */
1120Sstevel@tonic-gate 	NULL,			/* queue_head */
1130Sstevel@tonic-gate 	init_hash_table,	/* thr_hash_table */
1140Sstevel@tonic-gate 	1,			/* hash_size: size of the hash table */
1150Sstevel@tonic-gate 	0,			/* hash_mask: hash_size - 1 */
1160Sstevel@tonic-gate 	NULL,			/* ulwp_one */
1170Sstevel@tonic-gate 	NULL,			/* all_lwps */
1180Sstevel@tonic-gate 	NULL,			/* all_zombies */
1190Sstevel@tonic-gate 	0,			/* nthreads */
1200Sstevel@tonic-gate 	0,			/* nzombies */
1210Sstevel@tonic-gate 	0,			/* ndaemons */
1220Sstevel@tonic-gate 	0,			/* pid */
1230Sstevel@tonic-gate 	sigacthandler,		/* sigacthandler */
1240Sstevel@tonic-gate 	NULL,			/* lwp_stacks */
1250Sstevel@tonic-gate 	NULL,			/* lwp_laststack */
1260Sstevel@tonic-gate 	0,			/* nfreestack */
1270Sstevel@tonic-gate 	10,			/* thread_stack_cache */
1280Sstevel@tonic-gate 	NULL,			/* ulwp_freelist */
1290Sstevel@tonic-gate 	NULL,			/* ulwp_lastfree */
1300Sstevel@tonic-gate 	NULL,			/* ulwp_replace_free */
1310Sstevel@tonic-gate 	NULL,			/* ulwp_replace_last */
1320Sstevel@tonic-gate 	NULL,			/* atforklist */
1334574Sraf 	NULL,			/* robustlocks */
1349170SRoger.Faulkner@Sun.COM 	NULL,			/* robustlist */
1350Sstevel@tonic-gate 	NULL,			/* __tdb_bootstrap */
1360Sstevel@tonic-gate 	{			/* tdb */
1370Sstevel@tonic-gate 		NULL,		/* tdb_sync_addr_hash */
1380Sstevel@tonic-gate 		0,		/* tdb_register_count */
1390Sstevel@tonic-gate 		0,		/* tdb_hash_alloc_failed */
1400Sstevel@tonic-gate 		NULL,		/* tdb_sync_addr_free */
1410Sstevel@tonic-gate 		NULL,		/* tdb_sync_addr_last */
1420Sstevel@tonic-gate 		0,		/* tdb_sync_alloc */
1430Sstevel@tonic-gate 		{ 0, 0 },	/* tdb_ev_global_mask */
1440Sstevel@tonic-gate 		tdb_events,	/* tdb_events array */
1450Sstevel@tonic-gate 	},
1460Sstevel@tonic-gate };
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * The weak version is known to libc_db and mdb.
1500Sstevel@tonic-gate  */
1510Sstevel@tonic-gate #pragma weak _tdb_bootstrap = __tdb_bootstrap
1520Sstevel@tonic-gate uberdata_t **__tdb_bootstrap = NULL;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate int	thread_queue_fifo = 4;
1550Sstevel@tonic-gate int	thread_queue_dump = 0;
1560Sstevel@tonic-gate int	thread_cond_wait_defer = 0;
1570Sstevel@tonic-gate int	thread_error_detection = 0;
1580Sstevel@tonic-gate int	thread_async_safe = 0;
1590Sstevel@tonic-gate int	thread_stack_cache = 10;
1600Sstevel@tonic-gate int	thread_door_noreserve = 0;
1617255Sraf int	thread_locks_misaligned = 0;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static	ulwp_t	*ulwp_alloc(void);
1640Sstevel@tonic-gate static	void	ulwp_free(ulwp_t *);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Insert the lwp into the hash table.
1680Sstevel@tonic-gate  */
1690Sstevel@tonic-gate void
1700Sstevel@tonic-gate hash_in_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate 	ulwp->ul_hash = udp->thr_hash_table[ix].hash_bucket;
1730Sstevel@tonic-gate 	udp->thr_hash_table[ix].hash_bucket = ulwp;
1740Sstevel@tonic-gate 	ulwp->ul_ix = ix;
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate void
1780Sstevel@tonic-gate hash_in(ulwp_t *ulwp, uberdata_t *udp)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	int ix = TIDHASH(ulwp->ul_lwpid, udp);
1810Sstevel@tonic-gate 	mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	lmutex_lock(mp);
1840Sstevel@tonic-gate 	hash_in_unlocked(ulwp, ix, udp);
1850Sstevel@tonic-gate 	lmutex_unlock(mp);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate /*
1890Sstevel@tonic-gate  * Delete the lwp from the hash table.
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate void
1920Sstevel@tonic-gate hash_out_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	ulwp_t **ulwpp;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
1970Sstevel@tonic-gate 	    ulwp != *ulwpp;
1980Sstevel@tonic-gate 	    ulwpp = &(*ulwpp)->ul_hash)
1990Sstevel@tonic-gate 		;
2000Sstevel@tonic-gate 	*ulwpp = ulwp->ul_hash;
2010Sstevel@tonic-gate 	ulwp->ul_hash = NULL;
2020Sstevel@tonic-gate 	ulwp->ul_ix = -1;
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate void
2060Sstevel@tonic-gate hash_out(ulwp_t *ulwp, uberdata_t *udp)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	int ix;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if ((ix = ulwp->ul_ix) >= 0) {
2110Sstevel@tonic-gate 		mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		lmutex_lock(mp);
2140Sstevel@tonic-gate 		hash_out_unlocked(ulwp, ix, udp);
2150Sstevel@tonic-gate 		lmutex_unlock(mp);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2195891Sraf /*
2205891Sraf  * Retain stack information for thread structures that are being recycled for
2215891Sraf  * new threads.  All other members of the thread structure should be zeroed.
2225891Sraf  */
2230Sstevel@tonic-gate static void
2240Sstevel@tonic-gate ulwp_clean(ulwp_t *ulwp)
2250Sstevel@tonic-gate {
2265891Sraf 	caddr_t stk = ulwp->ul_stk;
2275891Sraf 	size_t mapsiz = ulwp->ul_mapsiz;
2285891Sraf 	size_t guardsize = ulwp->ul_guardsize;
2295891Sraf 	uintptr_t stktop = ulwp->ul_stktop;
2305891Sraf 	size_t stksiz = ulwp->ul_stksiz;
2315891Sraf 
2326515Sraf 	(void) memset(ulwp, 0, sizeof (*ulwp));
2335891Sraf 
2345891Sraf 	ulwp->ul_stk = stk;
2355891Sraf 	ulwp->ul_mapsiz = mapsiz;
2365891Sraf 	ulwp->ul_guardsize = guardsize;
2375891Sraf 	ulwp->ul_stktop = stktop;
2385891Sraf 	ulwp->ul_stksiz = stksiz;
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static int stackprot;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * Answer the question, "Is the lwp in question really dead?"
2450Sstevel@tonic-gate  * We must inquire of the operating system to be really sure
2460Sstevel@tonic-gate  * because the lwp may have called lwp_exit() but it has not
2470Sstevel@tonic-gate  * yet completed the exit.
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate static int
2500Sstevel@tonic-gate dead_and_buried(ulwp_t *ulwp)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	if (ulwp->ul_lwpid == (lwpid_t)(-1))
2530Sstevel@tonic-gate 		return (1);
2540Sstevel@tonic-gate 	if (ulwp->ul_dead && ulwp->ul_detached &&
2556812Sraf 	    _lwp_kill(ulwp->ul_lwpid, 0) == ESRCH) {
2560Sstevel@tonic-gate 		ulwp->ul_lwpid = (lwpid_t)(-1);
2570Sstevel@tonic-gate 		return (1);
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 	return (0);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate  * Attempt to keep the stack cache within the specified cache limit.
2640Sstevel@tonic-gate  */
2650Sstevel@tonic-gate static void
2660Sstevel@tonic-gate trim_stack_cache(int cache_limit)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	ulwp_t *self = curthread;
2690Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
2700Sstevel@tonic-gate 	ulwp_t *prev = NULL;
2710Sstevel@tonic-gate 	ulwp_t **ulwpp = &udp->lwp_stacks;
2720Sstevel@tonic-gate 	ulwp_t *ulwp;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, self));
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	while (udp->nfreestack > cache_limit && (ulwp = *ulwpp) != NULL) {
2770Sstevel@tonic-gate 		if (dead_and_buried(ulwp)) {
2780Sstevel@tonic-gate 			*ulwpp = ulwp->ul_next;
2790Sstevel@tonic-gate 			if (ulwp == udp->lwp_laststack)
2800Sstevel@tonic-gate 				udp->lwp_laststack = prev;
2810Sstevel@tonic-gate 			hash_out(ulwp, udp);
2820Sstevel@tonic-gate 			udp->nfreestack--;
2836515Sraf 			(void) munmap(ulwp->ul_stk, ulwp->ul_mapsiz);
2840Sstevel@tonic-gate 			/*
2850Sstevel@tonic-gate 			 * Now put the free ulwp on the ulwp freelist.
2860Sstevel@tonic-gate 			 */
2870Sstevel@tonic-gate 			ulwp->ul_mapsiz = 0;
2880Sstevel@tonic-gate 			ulwp->ul_next = NULL;
2890Sstevel@tonic-gate 			if (udp->ulwp_freelist == NULL)
2900Sstevel@tonic-gate 				udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
2910Sstevel@tonic-gate 			else {
2920Sstevel@tonic-gate 				udp->ulwp_lastfree->ul_next = ulwp;
2930Sstevel@tonic-gate 				udp->ulwp_lastfree = ulwp;
2940Sstevel@tonic-gate 			}
2950Sstevel@tonic-gate 		} else {
2960Sstevel@tonic-gate 			prev = ulwp;
2970Sstevel@tonic-gate 			ulwpp = &ulwp->ul_next;
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate  * Find an unused stack of the requested size
3040Sstevel@tonic-gate  * or create a new stack of the requested size.
3050Sstevel@tonic-gate  * Return a pointer to the ulwp_t structure referring to the stack, or NULL.
3060Sstevel@tonic-gate  * thr_exit() stores 1 in the ul_dead member.
3070Sstevel@tonic-gate  * thr_join() stores -1 in the ul_lwpid member.
3080Sstevel@tonic-gate  */
3098486SRoger.Faulkner@Sun.COM static ulwp_t *
3100Sstevel@tonic-gate find_stack(size_t stksize, size_t guardsize)
3110Sstevel@tonic-gate {
3121885Sraf 	static size_t pagesize = 0;
3131885Sraf 
3140Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
3150Sstevel@tonic-gate 	size_t mapsize;
3160Sstevel@tonic-gate 	ulwp_t *prev;
3170Sstevel@tonic-gate 	ulwp_t *ulwp;
3180Sstevel@tonic-gate 	ulwp_t **ulwpp;
3190Sstevel@tonic-gate 	void *stk;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * The stack is allocated PROT_READ|PROT_WRITE|PROT_EXEC
3230Sstevel@tonic-gate 	 * unless overridden by the system's configuration.
3240Sstevel@tonic-gate 	 */
3250Sstevel@tonic-gate 	if (stackprot == 0) {	/* do this once */
3260Sstevel@tonic-gate 		long lprot = _sysconf(_SC_STACK_PROT);
3270Sstevel@tonic-gate 		if (lprot <= 0)
3280Sstevel@tonic-gate 			lprot = (PROT_READ|PROT_WRITE|PROT_EXEC);
3290Sstevel@tonic-gate 		stackprot = (int)lprot;
3300Sstevel@tonic-gate 	}
3311885Sraf 	if (pagesize == 0)	/* do this once */
3321885Sraf 		pagesize = _sysconf(_SC_PAGESIZE);
3331885Sraf 
3340Sstevel@tonic-gate 	/*
3350Sstevel@tonic-gate 	 * One megabyte stacks by default, but subtract off
3360Sstevel@tonic-gate 	 * two pages for the system-created red zones.
3370Sstevel@tonic-gate 	 * Round up a non-zero stack size to a pagesize multiple.
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 	if (stksize == 0)
3401885Sraf 		stksize = DEFAULTSTACK - 2 * pagesize;
3410Sstevel@tonic-gate 	else
3421885Sraf 		stksize = ((stksize + pagesize - 1) & -pagesize);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/*
3450Sstevel@tonic-gate 	 * Round up the mapping size to a multiple of pagesize.
3460Sstevel@tonic-gate 	 * Note: mmap() provides at least one page of red zone
3470Sstevel@tonic-gate 	 * so we deduct that from the value of guardsize.
3480Sstevel@tonic-gate 	 */
3490Sstevel@tonic-gate 	if (guardsize != 0)
3501885Sraf 		guardsize = ((guardsize + pagesize - 1) & -pagesize) - pagesize;
3510Sstevel@tonic-gate 	mapsize = stksize + guardsize;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
3540Sstevel@tonic-gate 	for (prev = NULL, ulwpp = &udp->lwp_stacks;
3550Sstevel@tonic-gate 	    (ulwp = *ulwpp) != NULL;
3560Sstevel@tonic-gate 	    prev = ulwp, ulwpp = &ulwp->ul_next) {
3570Sstevel@tonic-gate 		if (ulwp->ul_mapsiz == mapsize &&
3580Sstevel@tonic-gate 		    ulwp->ul_guardsize == guardsize &&
3590Sstevel@tonic-gate 		    dead_and_buried(ulwp)) {
3600Sstevel@tonic-gate 			/*
3610Sstevel@tonic-gate 			 * The previous lwp is gone; reuse the stack.
3620Sstevel@tonic-gate 			 * Remove the ulwp from the stack list.
3630Sstevel@tonic-gate 			 */
3640Sstevel@tonic-gate 			*ulwpp = ulwp->ul_next;
3650Sstevel@tonic-gate 			ulwp->ul_next = NULL;
3660Sstevel@tonic-gate 			if (ulwp == udp->lwp_laststack)
3670Sstevel@tonic-gate 				udp->lwp_laststack = prev;
3680Sstevel@tonic-gate 			hash_out(ulwp, udp);
3690Sstevel@tonic-gate 			udp->nfreestack--;
3700Sstevel@tonic-gate 			lmutex_unlock(&udp->link_lock);
3710Sstevel@tonic-gate 			ulwp_clean(ulwp);
3720Sstevel@tonic-gate 			return (ulwp);
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 	}
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	/*
3770Sstevel@tonic-gate 	 * None of the cached stacks matched our mapping size.
3780Sstevel@tonic-gate 	 * Reduce the stack cache to get rid of possibly
3790Sstevel@tonic-gate 	 * very old stacks that will never be reused.
3800Sstevel@tonic-gate 	 */
3810Sstevel@tonic-gate 	if (udp->nfreestack > udp->thread_stack_cache)
3820Sstevel@tonic-gate 		trim_stack_cache(udp->thread_stack_cache);
3830Sstevel@tonic-gate 	else if (udp->nfreestack > 0)
3840Sstevel@tonic-gate 		trim_stack_cache(udp->nfreestack - 1);
3850Sstevel@tonic-gate 	lmutex_unlock(&udp->link_lock);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	/*
3880Sstevel@tonic-gate 	 * Create a new stack.
3890Sstevel@tonic-gate 	 */
3906515Sraf 	if ((stk = mmap(NULL, mapsize, stackprot,
3910Sstevel@tonic-gate 	    MAP_PRIVATE|MAP_NORESERVE|MAP_ANON, -1, (off_t)0)) != MAP_FAILED) {
3920Sstevel@tonic-gate 		/*
3930Sstevel@tonic-gate 		 * We have allocated our stack.  Now allocate the ulwp.
3940Sstevel@tonic-gate 		 */
3950Sstevel@tonic-gate 		ulwp = ulwp_alloc();
3960Sstevel@tonic-gate 		if (ulwp == NULL)
3976515Sraf 			(void) munmap(stk, mapsize);
3980Sstevel@tonic-gate 		else {
3990Sstevel@tonic-gate 			ulwp->ul_stk = stk;
4000Sstevel@tonic-gate 			ulwp->ul_mapsiz = mapsize;
4010Sstevel@tonic-gate 			ulwp->ul_guardsize = guardsize;
4020Sstevel@tonic-gate 			ulwp->ul_stktop = (uintptr_t)stk + mapsize;
4030Sstevel@tonic-gate 			ulwp->ul_stksiz = stksize;
4040Sstevel@tonic-gate 			if (guardsize)	/* protect the extra red zone */
4056515Sraf 				(void) mprotect(stk, guardsize, PROT_NONE);
4060Sstevel@tonic-gate 		}
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 	return (ulwp);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate /*
4120Sstevel@tonic-gate  * Get a ulwp_t structure from the free list or allocate a new one.
4130Sstevel@tonic-gate  * Such ulwp_t's do not have a stack allocated by the library.
4140Sstevel@tonic-gate  */
4150Sstevel@tonic-gate static ulwp_t *
4160Sstevel@tonic-gate ulwp_alloc(void)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	ulwp_t *self = curthread;
4190Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
4200Sstevel@tonic-gate 	size_t tls_size;
4210Sstevel@tonic-gate 	ulwp_t *prev;
4220Sstevel@tonic-gate 	ulwp_t *ulwp;
4230Sstevel@tonic-gate 	ulwp_t **ulwpp;
4240Sstevel@tonic-gate 	caddr_t data;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
4270Sstevel@tonic-gate 	for (prev = NULL, ulwpp = &udp->ulwp_freelist;
4280Sstevel@tonic-gate 	    (ulwp = *ulwpp) != NULL;
4290Sstevel@tonic-gate 	    prev = ulwp, ulwpp = &ulwp->ul_next) {
4300Sstevel@tonic-gate 		if (dead_and_buried(ulwp)) {
4310Sstevel@tonic-gate 			*ulwpp = ulwp->ul_next;
4320Sstevel@tonic-gate 			ulwp->ul_next = NULL;
4330Sstevel@tonic-gate 			if (ulwp == udp->ulwp_lastfree)
4340Sstevel@tonic-gate 				udp->ulwp_lastfree = prev;
4350Sstevel@tonic-gate 			hash_out(ulwp, udp);
4360Sstevel@tonic-gate 			lmutex_unlock(&udp->link_lock);
4370Sstevel@tonic-gate 			ulwp_clean(ulwp);
4380Sstevel@tonic-gate 			return (ulwp);
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 	lmutex_unlock(&udp->link_lock);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
4440Sstevel@tonic-gate 	data = lmalloc(sizeof (*ulwp) + tls_size);
4450Sstevel@tonic-gate 	if (data != NULL) {
4460Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
4470Sstevel@tonic-gate 		ulwp = (ulwp_t *)(data + tls_size);
4480Sstevel@tonic-gate 	}
4490Sstevel@tonic-gate 	return (ulwp);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate  * Free a ulwp structure.
4540Sstevel@tonic-gate  * If there is an associated stack, put it on the stack list and
4550Sstevel@tonic-gate  * munmap() previously freed stacks up to the residual cache limit.
4560Sstevel@tonic-gate  * Else put it on the ulwp free list and never call lfree() on it.
4570Sstevel@tonic-gate  */
4580Sstevel@tonic-gate static void
4590Sstevel@tonic-gate ulwp_free(ulwp_t *ulwp)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, curthread));
4640Sstevel@tonic-gate 	ulwp->ul_next = NULL;
4650Sstevel@tonic-gate 	if (ulwp == udp->ulwp_one)	/* don't reuse the primoridal stack */
4660Sstevel@tonic-gate 		/*EMPTY*/;
4670Sstevel@tonic-gate 	else if (ulwp->ul_mapsiz != 0) {
4680Sstevel@tonic-gate 		if (udp->lwp_stacks == NULL)
4690Sstevel@tonic-gate 			udp->lwp_stacks = udp->lwp_laststack = ulwp;
4700Sstevel@tonic-gate 		else {
4710Sstevel@tonic-gate 			udp->lwp_laststack->ul_next = ulwp;
4720Sstevel@tonic-gate 			udp->lwp_laststack = ulwp;
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 		if (++udp->nfreestack > udp->thread_stack_cache)
4750Sstevel@tonic-gate 			trim_stack_cache(udp->thread_stack_cache);
4760Sstevel@tonic-gate 	} else {
4770Sstevel@tonic-gate 		if (udp->ulwp_freelist == NULL)
4780Sstevel@tonic-gate 			udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
4790Sstevel@tonic-gate 		else {
4800Sstevel@tonic-gate 			udp->ulwp_lastfree->ul_next = ulwp;
4810Sstevel@tonic-gate 			udp->ulwp_lastfree = ulwp;
4820Sstevel@tonic-gate 		}
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate /*
4870Sstevel@tonic-gate  * Find a named lwp and return a pointer to its hash list location.
4880Sstevel@tonic-gate  * On success, returns with the hash lock held.
4890Sstevel@tonic-gate  */
4900Sstevel@tonic-gate ulwp_t **
4910Sstevel@tonic-gate find_lwpp(thread_t tid)
4920Sstevel@tonic-gate {
4930Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
4940Sstevel@tonic-gate 	int ix = TIDHASH(tid, udp);
4950Sstevel@tonic-gate 	mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
4960Sstevel@tonic-gate 	ulwp_t *ulwp;
4970Sstevel@tonic-gate 	ulwp_t **ulwpp;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if (tid == 0)
5000Sstevel@tonic-gate 		return (NULL);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	lmutex_lock(mp);
5030Sstevel@tonic-gate 	for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
5040Sstevel@tonic-gate 	    (ulwp = *ulwpp) != NULL;
5050Sstevel@tonic-gate 	    ulwpp = &ulwp->ul_hash) {
5060Sstevel@tonic-gate 		if (ulwp->ul_lwpid == tid)
5070Sstevel@tonic-gate 			return (ulwpp);
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 	lmutex_unlock(mp);
5100Sstevel@tonic-gate 	return (NULL);
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate /*
5140Sstevel@tonic-gate  * Wake up all lwps waiting on this lwp for some reason.
5150Sstevel@tonic-gate  */
5160Sstevel@tonic-gate void
5170Sstevel@tonic-gate ulwp_broadcast(ulwp_t *ulwp)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate 	ulwp_t *self = curthread;
5200Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
5236812Sraf 	(void) cond_broadcast(ulwp_condvar(ulwp, udp));
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate  * Find a named lwp and return a pointer to it.
5280Sstevel@tonic-gate  * Returns with the hash lock held.
5290Sstevel@tonic-gate  */
5300Sstevel@tonic-gate ulwp_t *
5310Sstevel@tonic-gate find_lwp(thread_t tid)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate 	ulwp_t *self = curthread;
5340Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
5350Sstevel@tonic-gate 	ulwp_t *ulwp = NULL;
5360Sstevel@tonic-gate 	ulwp_t **ulwpp;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	if (self->ul_lwpid == tid) {
5390Sstevel@tonic-gate 		ulwp = self;
5400Sstevel@tonic-gate 		ulwp_lock(ulwp, udp);
5410Sstevel@tonic-gate 	} else if ((ulwpp = find_lwpp(tid)) != NULL) {
5420Sstevel@tonic-gate 		ulwp = *ulwpp;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if (ulwp && ulwp->ul_dead) {
5460Sstevel@tonic-gate 		ulwp_unlock(ulwp, udp);
5470Sstevel@tonic-gate 		ulwp = NULL;
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	return (ulwp);
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate int
5540Sstevel@tonic-gate _thrp_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
5556247Sraf 	long flags, thread_t *new_thread, size_t guardsize)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	ulwp_t *self = curthread;
5580Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
5590Sstevel@tonic-gate 	ucontext_t uc;
5600Sstevel@tonic-gate 	uint_t lwp_flags;
5610Sstevel@tonic-gate 	thread_t tid;
5627657SRoger.Faulkner@Sun.COM 	int error;
5630Sstevel@tonic-gate 	ulwp_t *ulwp;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/*
5660Sstevel@tonic-gate 	 * Enforce the restriction of not creating any threads
5670Sstevel@tonic-gate 	 * until the primary link map has been initialized.
5680Sstevel@tonic-gate 	 * Also, disallow thread creation to a child of vfork().
5690Sstevel@tonic-gate 	 */
5700Sstevel@tonic-gate 	if (!self->ul_primarymap || self->ul_vfork)
5710Sstevel@tonic-gate 		return (ENOTSUP);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if (udp->hash_size == 1)
5740Sstevel@tonic-gate 		finish_init();
5750Sstevel@tonic-gate 
5766247Sraf 	if ((stk || stksize) && stksize < MINSTACK)
5770Sstevel@tonic-gate 		return (EINVAL);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if (stk == NULL) {
5800Sstevel@tonic-gate 		if ((ulwp = find_stack(stksize, guardsize)) == NULL)
5810Sstevel@tonic-gate 			return (ENOMEM);
5820Sstevel@tonic-gate 		stksize = ulwp->ul_mapsiz - ulwp->ul_guardsize;
5830Sstevel@tonic-gate 	} else {
5840Sstevel@tonic-gate 		/* initialize the private stack */
5850Sstevel@tonic-gate 		if ((ulwp = ulwp_alloc()) == NULL)
5860Sstevel@tonic-gate 			return (ENOMEM);
5870Sstevel@tonic-gate 		ulwp->ul_stk = stk;
5880Sstevel@tonic-gate 		ulwp->ul_stktop = (uintptr_t)stk + stksize;
5890Sstevel@tonic-gate 		ulwp->ul_stksiz = stksize;
5900Sstevel@tonic-gate 	}
5918486SRoger.Faulkner@Sun.COM 	/* ulwp is not in the hash table; make sure hash_out() doesn't fail */
5928486SRoger.Faulkner@Sun.COM 	ulwp->ul_ix = -1;
5930Sstevel@tonic-gate 	ulwp->ul_errnop = &ulwp->ul_errno;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	lwp_flags = LWP_SUSPENDED;
5960Sstevel@tonic-gate 	if (flags & (THR_DETACHED|THR_DAEMON)) {
5970Sstevel@tonic-gate 		flags |= THR_DETACHED;
5980Sstevel@tonic-gate 		lwp_flags |= LWP_DETACHED;
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 	if (flags & THR_DAEMON)
6010Sstevel@tonic-gate 		lwp_flags |= LWP_DAEMON;
6020Sstevel@tonic-gate 
6036812Sraf 	/* creating a thread: enforce mt-correctness in mutex_lock() */
6040Sstevel@tonic-gate 	self->ul_async_safe = 1;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	/* per-thread copies of global variables, for speed */
6070Sstevel@tonic-gate 	ulwp->ul_queue_fifo = self->ul_queue_fifo;
6080Sstevel@tonic-gate 	ulwp->ul_cond_wait_defer = self->ul_cond_wait_defer;
6090Sstevel@tonic-gate 	ulwp->ul_error_detection = self->ul_error_detection;
6100Sstevel@tonic-gate 	ulwp->ul_async_safe = self->ul_async_safe;
6110Sstevel@tonic-gate 	ulwp->ul_max_spinners = self->ul_max_spinners;
6120Sstevel@tonic-gate 	ulwp->ul_adaptive_spin = self->ul_adaptive_spin;
6130Sstevel@tonic-gate 	ulwp->ul_queue_spin = self->ul_queue_spin;
6140Sstevel@tonic-gate 	ulwp->ul_door_noreserve = self->ul_door_noreserve;
6157255Sraf 	ulwp->ul_misaligned = self->ul_misaligned;
6160Sstevel@tonic-gate 
6176247Sraf 	/* new thread inherits creating thread's scheduling parameters */
6186247Sraf 	ulwp->ul_policy = self->ul_policy;
6196247Sraf 	ulwp->ul_pri = (self->ul_epri? self->ul_epri : self->ul_pri);
6206247Sraf 	ulwp->ul_cid = self->ul_cid;
6216247Sraf 	ulwp->ul_rtclassid = self->ul_rtclassid;
6226247Sraf 
6230Sstevel@tonic-gate 	ulwp->ul_primarymap = self->ul_primarymap;
6240Sstevel@tonic-gate 	ulwp->ul_self = ulwp;
6250Sstevel@tonic-gate 	ulwp->ul_uberdata = udp;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/* debugger support */
6280Sstevel@tonic-gate 	ulwp->ul_usropts = flags;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate #ifdef __sparc
6310Sstevel@tonic-gate 	/*
6320Sstevel@tonic-gate 	 * We cache several instructions in the thread structure for use
6330Sstevel@tonic-gate 	 * by the fasttrap DTrace provider. When changing this, read the
6340Sstevel@tonic-gate 	 * comment in fasttrap.h for the all the other places that must
6350Sstevel@tonic-gate 	 * be changed.
6360Sstevel@tonic-gate 	 */
6370Sstevel@tonic-gate 	ulwp->ul_dsave = 0x9de04000;	/* save %g1, %g0, %sp */
6380Sstevel@tonic-gate 	ulwp->ul_drestore = 0x81e80000;	/* restore %g0, %g0, %g0 */
6390Sstevel@tonic-gate 	ulwp->ul_dftret = 0x91d0203a;	/* ta 0x3a */
6400Sstevel@tonic-gate 	ulwp->ul_dreturn = 0x81ca0000;	/* return %o0 */
6410Sstevel@tonic-gate #endif
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	ulwp->ul_startpc = func;
6440Sstevel@tonic-gate 	ulwp->ul_startarg = arg;
6450Sstevel@tonic-gate 	_fpinherit(ulwp);
6460Sstevel@tonic-gate 	/*
6470Sstevel@tonic-gate 	 * Defer signals on the new thread until its TLS constructors
6486812Sraf 	 * have been called.  _thrp_setup() will call sigon() after
6490Sstevel@tonic-gate 	 * it has called tls_setup().
6500Sstevel@tonic-gate 	 */
6510Sstevel@tonic-gate 	ulwp->ul_sigdefer = 1;
6520Sstevel@tonic-gate 
6537657SRoger.Faulkner@Sun.COM 	error = setup_context(&uc, _thrp_setup, ulwp,
6547657SRoger.Faulkner@Sun.COM 	    (caddr_t)ulwp->ul_stk + ulwp->ul_guardsize, stksize);
6557657SRoger.Faulkner@Sun.COM 	if (error != 0 && stk != NULL)	/* inaccessible stack */
6567657SRoger.Faulkner@Sun.COM 		error = EFAULT;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	/*
6590Sstevel@tonic-gate 	 * Call enter_critical() to avoid being suspended until we
6600Sstevel@tonic-gate 	 * have linked the new thread into the proper lists.
6610Sstevel@tonic-gate 	 * This is necessary because forkall() and fork1() must
6620Sstevel@tonic-gate 	 * suspend all threads and they must see a complete list.
6630Sstevel@tonic-gate 	 */
6640Sstevel@tonic-gate 	enter_critical(self);
6650Sstevel@tonic-gate 	uc.uc_sigmask = ulwp->ul_sigmask = self->ul_sigmask;
6660Sstevel@tonic-gate 	if (error != 0 ||
6670Sstevel@tonic-gate 	    (error = __lwp_create(&uc, lwp_flags, &tid)) != 0) {
6680Sstevel@tonic-gate 		exit_critical(self);
6690Sstevel@tonic-gate 		ulwp->ul_lwpid = (lwpid_t)(-1);
6700Sstevel@tonic-gate 		ulwp->ul_dead = 1;
6710Sstevel@tonic-gate 		ulwp->ul_detached = 1;
6720Sstevel@tonic-gate 		lmutex_lock(&udp->link_lock);
6730Sstevel@tonic-gate 		ulwp_free(ulwp);
6740Sstevel@tonic-gate 		lmutex_unlock(&udp->link_lock);
6750Sstevel@tonic-gate 		return (error);
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 	self->ul_nocancel = 0;	/* cancellation is now possible */
6780Sstevel@tonic-gate 	udp->uberflags.uf_mt = 1;
6790Sstevel@tonic-gate 	if (new_thread)
6800Sstevel@tonic-gate 		*new_thread = tid;
6810Sstevel@tonic-gate 	if (flags & THR_DETACHED)
6820Sstevel@tonic-gate 		ulwp->ul_detached = 1;
6830Sstevel@tonic-gate 	ulwp->ul_lwpid = tid;
6840Sstevel@tonic-gate 	ulwp->ul_stop = TSTP_REGULAR;
6853512Sraf 	if (flags & THR_SUSPENDED)
6863512Sraf 		ulwp->ul_created = 1;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
6890Sstevel@tonic-gate 	ulwp->ul_forw = udp->all_lwps;
6900Sstevel@tonic-gate 	ulwp->ul_back = udp->all_lwps->ul_back;
6910Sstevel@tonic-gate 	ulwp->ul_back->ul_forw = ulwp;
6920Sstevel@tonic-gate 	ulwp->ul_forw->ul_back = ulwp;
6930Sstevel@tonic-gate 	hash_in(ulwp, udp);
6940Sstevel@tonic-gate 	udp->nthreads++;
6950Sstevel@tonic-gate 	if (flags & THR_DAEMON)
6960Sstevel@tonic-gate 		udp->ndaemons++;
6970Sstevel@tonic-gate 	if (flags & THR_NEW_LWP)
6980Sstevel@tonic-gate 		thr_concurrency++;
6991672Sraf 	__libc_threaded = 1;		/* inform stdio */
7000Sstevel@tonic-gate 	lmutex_unlock(&udp->link_lock);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	if (__td_event_report(self, TD_CREATE, udp)) {
7030Sstevel@tonic-gate 		self->ul_td_evbuf.eventnum = TD_CREATE;
7040Sstevel@tonic-gate 		self->ul_td_evbuf.eventdata = (void *)(uintptr_t)tid;
7050Sstevel@tonic-gate 		tdb_event(TD_CREATE, udp);
7060Sstevel@tonic-gate 	}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	exit_critical(self);
7093512Sraf 
7103512Sraf 	if (!(flags & THR_SUSPENDED))
7113512Sraf 		(void) _thrp_continue(tid, TSTP_REGULAR);
7123512Sraf 
7130Sstevel@tonic-gate 	return (0);
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate int
7176812Sraf thr_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
7180Sstevel@tonic-gate 	long flags, thread_t *new_thread)
7190Sstevel@tonic-gate {
7206247Sraf 	return (_thrp_create(stk, stksize, func, arg, flags, new_thread, 0));
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /*
7240Sstevel@tonic-gate  * A special cancellation cleanup hook for DCE.
7250Sstevel@tonic-gate  * cleanuphndlr, when it is not NULL, will contain a callback
7260Sstevel@tonic-gate  * function to be called before a thread is terminated in
7276812Sraf  * thr_exit() as a result of being cancelled.
7280Sstevel@tonic-gate  */
7290Sstevel@tonic-gate static void (*cleanuphndlr)(void) = NULL;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate /*
7320Sstevel@tonic-gate  * _pthread_setcleanupinit: sets the cleanup hook.
7330Sstevel@tonic-gate  */
7340Sstevel@tonic-gate int
7350Sstevel@tonic-gate _pthread_setcleanupinit(void (*func)(void))
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	cleanuphndlr = func;
7380Sstevel@tonic-gate 	return (0);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate void
7420Sstevel@tonic-gate _thrp_exit()
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate 	ulwp_t *self = curthread;
7450Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
7460Sstevel@tonic-gate 	ulwp_t *replace = NULL;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	if (__td_event_report(self, TD_DEATH, udp)) {
7490Sstevel@tonic-gate 		self->ul_td_evbuf.eventnum = TD_DEATH;
7500Sstevel@tonic-gate 		tdb_event(TD_DEATH, udp);
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer != 0);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
7560Sstevel@tonic-gate 	udp->nthreads--;
7570Sstevel@tonic-gate 	if (self->ul_usropts & THR_NEW_LWP)
7580Sstevel@tonic-gate 		thr_concurrency--;
7590Sstevel@tonic-gate 	if (self->ul_usropts & THR_DAEMON)
7600Sstevel@tonic-gate 		udp->ndaemons--;
7610Sstevel@tonic-gate 	else if (udp->nthreads == udp->ndaemons) {
7620Sstevel@tonic-gate 		/*
7630Sstevel@tonic-gate 		 * We are the last non-daemon thread exiting.
7640Sstevel@tonic-gate 		 * Exit the process.  We retain our TSD and TLS so
7650Sstevel@tonic-gate 		 * that atexit() application functions can use them.
7660Sstevel@tonic-gate 		 */
7670Sstevel@tonic-gate 		lmutex_unlock(&udp->link_lock);
7680Sstevel@tonic-gate 		exit(0);
7690Sstevel@tonic-gate 		thr_panic("_thrp_exit(): exit(0) returned");
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 	lmutex_unlock(&udp->link_lock);
7720Sstevel@tonic-gate 
7734574Sraf 	tsd_exit();		/* deallocate thread-specific data */
7744574Sraf 	tls_exit();		/* deallocate thread-local storage */
7754574Sraf 	heldlock_exit();	/* deal with left-over held locks */
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	/* block all signals to finish exiting */
7780Sstevel@tonic-gate 	block_all_signals(self);
7790Sstevel@tonic-gate 	/* also prevent ourself from being suspended */
7800Sstevel@tonic-gate 	enter_critical(self);
7810Sstevel@tonic-gate 	rwl_free(self);
7820Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
7830Sstevel@tonic-gate 	ulwp_free(self);
7840Sstevel@tonic-gate 	(void) ulwp_lock(self, udp);
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	if (self->ul_mapsiz && !self->ul_detached) {
7870Sstevel@tonic-gate 		/*
7880Sstevel@tonic-gate 		 * We want to free the stack for reuse but must keep
7890Sstevel@tonic-gate 		 * the ulwp_t struct for the benefit of thr_join().
7900Sstevel@tonic-gate 		 * For this purpose we allocate a replacement ulwp_t.
7910Sstevel@tonic-gate 		 */
7920Sstevel@tonic-gate 		if ((replace = udp->ulwp_replace_free) == NULL)
7930Sstevel@tonic-gate 			replace = lmalloc(REPLACEMENT_SIZE);
7940Sstevel@tonic-gate 		else if ((udp->ulwp_replace_free = replace->ul_next) == NULL)
7950Sstevel@tonic-gate 			udp->ulwp_replace_last = NULL;
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	if (udp->all_lwps == self)
7990Sstevel@tonic-gate 		udp->all_lwps = self->ul_forw;
8000Sstevel@tonic-gate 	if (udp->all_lwps == self)
8010Sstevel@tonic-gate 		udp->all_lwps = NULL;
8020Sstevel@tonic-gate 	else {
8030Sstevel@tonic-gate 		self->ul_forw->ul_back = self->ul_back;
8040Sstevel@tonic-gate 		self->ul_back->ul_forw = self->ul_forw;
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 	self->ul_forw = self->ul_back = NULL;
8076247Sraf #if defined(THREAD_DEBUG)
8080Sstevel@tonic-gate 	/* collect queue lock statistics before marking ourself dead */
8090Sstevel@tonic-gate 	record_spin_locks(self);
8106247Sraf #endif
8110Sstevel@tonic-gate 	self->ul_dead = 1;
8120Sstevel@tonic-gate 	self->ul_pleasestop = 0;
8130Sstevel@tonic-gate 	if (replace != NULL) {
8140Sstevel@tonic-gate 		int ix = self->ul_ix;		/* the hash index */
8156515Sraf 		(void) memcpy(replace, self, REPLACEMENT_SIZE);
8160Sstevel@tonic-gate 		replace->ul_self = replace;
8170Sstevel@tonic-gate 		replace->ul_next = NULL;	/* clone not on stack list */
8180Sstevel@tonic-gate 		replace->ul_mapsiz = 0;		/* allows clone to be freed */
8190Sstevel@tonic-gate 		replace->ul_replace = 1;	/* requires clone to be freed */
8200Sstevel@tonic-gate 		hash_out_unlocked(self, ix, udp);
8210Sstevel@tonic-gate 		hash_in_unlocked(replace, ix, udp);
8220Sstevel@tonic-gate 		ASSERT(!(self->ul_detached));
8230Sstevel@tonic-gate 		self->ul_detached = 1;		/* this frees the stack */
8240Sstevel@tonic-gate 		self->ul_schedctl = NULL;
8250Sstevel@tonic-gate 		self->ul_schedctl_called = &udp->uberflags;
8260Sstevel@tonic-gate 		set_curthread(self = replace);
8270Sstevel@tonic-gate 		/*
8280Sstevel@tonic-gate 		 * Having just changed the address of curthread, we
8290Sstevel@tonic-gate 		 * must reset the ownership of the locks we hold so
8300Sstevel@tonic-gate 		 * that assertions will not fire when we release them.
8310Sstevel@tonic-gate 		 */
8320Sstevel@tonic-gate 		udp->link_lock.mutex_owner = (uintptr_t)self;
8330Sstevel@tonic-gate 		ulwp_mutex(self, udp)->mutex_owner = (uintptr_t)self;
8340Sstevel@tonic-gate 		/*
8350Sstevel@tonic-gate 		 * NOTE:
8360Sstevel@tonic-gate 		 * On i386, %gs still references the original, not the
8370Sstevel@tonic-gate 		 * replacement, ulwp structure.  Fetching the replacement
8380Sstevel@tonic-gate 		 * curthread pointer via %gs:0 works correctly since the
8390Sstevel@tonic-gate 		 * original ulwp structure will not be reallocated until
8400Sstevel@tonic-gate 		 * this lwp has completed its lwp_exit() system call (see
8410Sstevel@tonic-gate 		 * dead_and_buried()), but from here on out, we must make
8420Sstevel@tonic-gate 		 * no references to %gs:<offset> other than %gs:0.
8430Sstevel@tonic-gate 		 */
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 	/*
8460Sstevel@tonic-gate 	 * Put non-detached terminated threads in the all_zombies list.
8470Sstevel@tonic-gate 	 */
8480Sstevel@tonic-gate 	if (!self->ul_detached) {
8490Sstevel@tonic-gate 		udp->nzombies++;
8500Sstevel@tonic-gate 		if (udp->all_zombies == NULL) {
8510Sstevel@tonic-gate 			ASSERT(udp->nzombies == 1);
8520Sstevel@tonic-gate 			udp->all_zombies = self->ul_forw = self->ul_back = self;
8530Sstevel@tonic-gate 		} else {
8540Sstevel@tonic-gate 			self->ul_forw = udp->all_zombies;
8550Sstevel@tonic-gate 			self->ul_back = udp->all_zombies->ul_back;
8560Sstevel@tonic-gate 			self->ul_back->ul_forw = self;
8570Sstevel@tonic-gate 			self->ul_forw->ul_back = self;
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 	/*
8610Sstevel@tonic-gate 	 * Notify everyone waiting for this thread.
8620Sstevel@tonic-gate 	 */
8630Sstevel@tonic-gate 	ulwp_broadcast(self);
8640Sstevel@tonic-gate 	(void) ulwp_unlock(self, udp);
8650Sstevel@tonic-gate 	/*
8660Sstevel@tonic-gate 	 * Prevent any more references to the schedctl data.
8670Sstevel@tonic-gate 	 * We are exiting and continue_fork() may not find us.
8680Sstevel@tonic-gate 	 * Do this just before dropping link_lock, since fork
8690Sstevel@tonic-gate 	 * serializes on link_lock.
8700Sstevel@tonic-gate 	 */
8710Sstevel@tonic-gate 	self->ul_schedctl = NULL;
8720Sstevel@tonic-gate 	self->ul_schedctl_called = &udp->uberflags;
8730Sstevel@tonic-gate 	lmutex_unlock(&udp->link_lock);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	ASSERT(self->ul_critical == 1);
8760Sstevel@tonic-gate 	ASSERT(self->ul_preempt == 0);
8770Sstevel@tonic-gate 	_lwp_terminate();	/* never returns */
8780Sstevel@tonic-gate 	thr_panic("_thrp_exit(): _lwp_terminate() returned");
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate 
8816247Sraf #if defined(THREAD_DEBUG)
8820Sstevel@tonic-gate void
8830Sstevel@tonic-gate collect_queue_statistics()
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
8860Sstevel@tonic-gate 	ulwp_t *ulwp;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	if (thread_queue_dump) {
8890Sstevel@tonic-gate 		lmutex_lock(&udp->link_lock);
8900Sstevel@tonic-gate 		if ((ulwp = udp->all_lwps) != NULL) {
8910Sstevel@tonic-gate 			do {
8920Sstevel@tonic-gate 				record_spin_locks(ulwp);
8930Sstevel@tonic-gate 			} while ((ulwp = ulwp->ul_forw) != udp->all_lwps);
8940Sstevel@tonic-gate 		}
8950Sstevel@tonic-gate 		lmutex_unlock(&udp->link_lock);
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate }
8986247Sraf #endif
8990Sstevel@tonic-gate 
9006812Sraf static void __NORETURN
9016812Sraf _thrp_exit_common(void *status, int unwind)
9020Sstevel@tonic-gate {
9030Sstevel@tonic-gate 	ulwp_t *self = curthread;
9040Sstevel@tonic-gate 	int cancelled = (self->ul_cancel_pending && status == PTHREAD_CANCELED);
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0 && self->ul_preempt == 0);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	/*
9090Sstevel@tonic-gate 	 * Disable cancellation and call the special DCE cancellation
9100Sstevel@tonic-gate 	 * cleanup hook if it is enabled.  Do nothing else before calling
9110Sstevel@tonic-gate 	 * the DCE cancellation cleanup hook; it may call longjmp() and
9120Sstevel@tonic-gate 	 * never return here.
9130Sstevel@tonic-gate 	 */
9140Sstevel@tonic-gate 	self->ul_cancel_disabled = 1;
9150Sstevel@tonic-gate 	self->ul_cancel_async = 0;
9160Sstevel@tonic-gate 	self->ul_save_async = 0;
9170Sstevel@tonic-gate 	self->ul_cancelable = 0;
9180Sstevel@tonic-gate 	self->ul_cancel_pending = 0;
9195891Sraf 	set_cancel_pending_flag(self, 1);
9200Sstevel@tonic-gate 	if (cancelled && cleanuphndlr != NULL)
9210Sstevel@tonic-gate 		(*cleanuphndlr)();
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	/*
9240Sstevel@tonic-gate 	 * Block application signals while we are exiting.
9250Sstevel@tonic-gate 	 * We call out to C++, TSD, and TLS destructors while exiting
9260Sstevel@tonic-gate 	 * and these are application-defined, so we cannot be assured
9270Sstevel@tonic-gate 	 * that they won't reset the signal mask.  We use sigoff() to
9280Sstevel@tonic-gate 	 * defer any signals that may be received as a result of this
9290Sstevel@tonic-gate 	 * bad behavior.  Such signals will be lost to the process
9300Sstevel@tonic-gate 	 * when the thread finishes exiting.
9310Sstevel@tonic-gate 	 */
9326812Sraf 	(void) thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
9330Sstevel@tonic-gate 	sigoff(self);
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	self->ul_rval = status;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	/*
9380Sstevel@tonic-gate 	 * If thr_exit is being called from the places where
9390Sstevel@tonic-gate 	 * C++ destructors are to be called such as cancellation
9400Sstevel@tonic-gate 	 * points, then set this flag. It is checked in _t_cancel()
9410Sstevel@tonic-gate 	 * to decide whether _ex_unwind() is to be called or not.
9420Sstevel@tonic-gate 	 */
9430Sstevel@tonic-gate 	if (unwind)
9440Sstevel@tonic-gate 		self->ul_unwind = 1;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	/*
9470Sstevel@tonic-gate 	 * _thrp_unwind() will eventually call _thrp_exit().
9480Sstevel@tonic-gate 	 * It never returns.
9490Sstevel@tonic-gate 	 */
9500Sstevel@tonic-gate 	_thrp_unwind(NULL);
9516812Sraf 	thr_panic("_thrp_exit_common(): _thrp_unwind() returned");
9526812Sraf 
9536812Sraf 	for (;;)	/* to shut the compiler up about __NORETURN */
9546812Sraf 		continue;
9550Sstevel@tonic-gate }
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate /*
9580Sstevel@tonic-gate  * Called when a thread returns from its start function.
9590Sstevel@tonic-gate  * We are at the top of the stack; no unwinding is necessary.
9600Sstevel@tonic-gate  */
9610Sstevel@tonic-gate void
9626812Sraf _thrp_terminate(void *status)
9630Sstevel@tonic-gate {
9646812Sraf 	_thrp_exit_common(status, 0);
9650Sstevel@tonic-gate }
9660Sstevel@tonic-gate 
9676812Sraf #pragma weak pthread_exit = thr_exit
9686812Sraf #pragma weak _thr_exit = thr_exit
9690Sstevel@tonic-gate void
9706812Sraf thr_exit(void *status)
9710Sstevel@tonic-gate {
9726812Sraf 	_thrp_exit_common(status, 1);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate int
9760Sstevel@tonic-gate _thrp_join(thread_t tid, thread_t *departed, void **status, int do_cancel)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
9790Sstevel@tonic-gate 	mutex_t *mp;
9800Sstevel@tonic-gate 	void *rval;
9810Sstevel@tonic-gate 	thread_t found;
9820Sstevel@tonic-gate 	ulwp_t *ulwp;
9830Sstevel@tonic-gate 	ulwp_t **ulwpp;
9840Sstevel@tonic-gate 	int replace;
9850Sstevel@tonic-gate 	int error;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	if (do_cancel)
9880Sstevel@tonic-gate 		error = lwp_wait(tid, &found);
9890Sstevel@tonic-gate 	else {
9900Sstevel@tonic-gate 		while ((error = __lwp_wait(tid, &found)) == EINTR)
9910Sstevel@tonic-gate 			;
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 	if (error)
9940Sstevel@tonic-gate 		return (error);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * We must hold link_lock to avoid a race condition with find_stack().
9980Sstevel@tonic-gate 	 */
9990Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
10000Sstevel@tonic-gate 	if ((ulwpp = find_lwpp(found)) == NULL) {
10010Sstevel@tonic-gate 		/*
10020Sstevel@tonic-gate 		 * lwp_wait() found an lwp that the library doesn't know
10030Sstevel@tonic-gate 		 * about.  It must have been created with _lwp_create().
10040Sstevel@tonic-gate 		 * Just return its lwpid; we can't know its status.
10050Sstevel@tonic-gate 		 */
10060Sstevel@tonic-gate 		lmutex_unlock(&udp->link_lock);
10070Sstevel@tonic-gate 		rval = NULL;
10080Sstevel@tonic-gate 	} else {
10090Sstevel@tonic-gate 		/*
10100Sstevel@tonic-gate 		 * Remove ulwp from the hash table.
10110Sstevel@tonic-gate 		 */
10120Sstevel@tonic-gate 		ulwp = *ulwpp;
10130Sstevel@tonic-gate 		*ulwpp = ulwp->ul_hash;
10140Sstevel@tonic-gate 		ulwp->ul_hash = NULL;
10150Sstevel@tonic-gate 		/*
10160Sstevel@tonic-gate 		 * Remove ulwp from all_zombies list.
10170Sstevel@tonic-gate 		 */
10180Sstevel@tonic-gate 		ASSERT(udp->nzombies >= 1);
10190Sstevel@tonic-gate 		if (udp->all_zombies == ulwp)
10200Sstevel@tonic-gate 			udp->all_zombies = ulwp->ul_forw;
10210Sstevel@tonic-gate 		if (udp->all_zombies == ulwp)
10220Sstevel@tonic-gate 			udp->all_zombies = NULL;
10230Sstevel@tonic-gate 		else {
10240Sstevel@tonic-gate 			ulwp->ul_forw->ul_back = ulwp->ul_back;
10250Sstevel@tonic-gate 			ulwp->ul_back->ul_forw = ulwp->ul_forw;
10260Sstevel@tonic-gate 		}
10270Sstevel@tonic-gate 		ulwp->ul_forw = ulwp->ul_back = NULL;
10280Sstevel@tonic-gate 		udp->nzombies--;
10290Sstevel@tonic-gate 		ASSERT(ulwp->ul_dead && !ulwp->ul_detached &&
10304843Sraf 		    !(ulwp->ul_usropts & (THR_DETACHED|THR_DAEMON)));
10310Sstevel@tonic-gate 		/*
10320Sstevel@tonic-gate 		 * We can't call ulwp_unlock(ulwp) after we set
10330Sstevel@tonic-gate 		 * ulwp->ul_ix = -1 so we have to get a pointer to the
10340Sstevel@tonic-gate 		 * ulwp's hash table mutex now in order to unlock it below.
10350Sstevel@tonic-gate 		 */
10360Sstevel@tonic-gate 		mp = ulwp_mutex(ulwp, udp);
10370Sstevel@tonic-gate 		ulwp->ul_lwpid = (lwpid_t)(-1);
10380Sstevel@tonic-gate 		ulwp->ul_ix = -1;
10390Sstevel@tonic-gate 		rval = ulwp->ul_rval;
10400Sstevel@tonic-gate 		replace = ulwp->ul_replace;
10410Sstevel@tonic-gate 		lmutex_unlock(mp);
10420Sstevel@tonic-gate 		if (replace) {
10430Sstevel@tonic-gate 			ulwp->ul_next = NULL;
10440Sstevel@tonic-gate 			if (udp->ulwp_replace_free == NULL)
10450Sstevel@tonic-gate 				udp->ulwp_replace_free =
10464843Sraf 				    udp->ulwp_replace_last = ulwp;
10470Sstevel@tonic-gate 			else {
10480Sstevel@tonic-gate 				udp->ulwp_replace_last->ul_next = ulwp;
10490Sstevel@tonic-gate 				udp->ulwp_replace_last = ulwp;
10500Sstevel@tonic-gate 			}
10510Sstevel@tonic-gate 		}
10520Sstevel@tonic-gate 		lmutex_unlock(&udp->link_lock);
10530Sstevel@tonic-gate 	}
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	if (departed != NULL)
10560Sstevel@tonic-gate 		*departed = found;
10570Sstevel@tonic-gate 	if (status != NULL)
10580Sstevel@tonic-gate 		*status = rval;
10590Sstevel@tonic-gate 	return (0);
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate int
10636812Sraf thr_join(thread_t tid, thread_t *departed, void **status)
10640Sstevel@tonic-gate {
10650Sstevel@tonic-gate 	int error = _thrp_join(tid, departed, status, 1);
10660Sstevel@tonic-gate 	return ((error == EINVAL)? ESRCH : error);
10670Sstevel@tonic-gate }
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate /*
10700Sstevel@tonic-gate  * pthread_join() differs from Solaris thr_join():
10710Sstevel@tonic-gate  * It does not return the departed thread's id
10720Sstevel@tonic-gate  * and hence does not have a "departed" argument.
10730Sstevel@tonic-gate  * It returns EINVAL if tid refers to a detached thread.
10740Sstevel@tonic-gate  */
10756812Sraf #pragma weak _pthread_join = pthread_join
10760Sstevel@tonic-gate int
10776812Sraf pthread_join(pthread_t tid, void **status)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate 	return ((tid == 0)? ESRCH : _thrp_join(tid, NULL, status, 1));
10800Sstevel@tonic-gate }
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate int
10836812Sraf pthread_detach(pthread_t tid)
10840Sstevel@tonic-gate {
10850Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
10860Sstevel@tonic-gate 	ulwp_t *ulwp;
10870Sstevel@tonic-gate 	ulwp_t **ulwpp;
10880Sstevel@tonic-gate 	int error = 0;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	if ((ulwpp = find_lwpp(tid)) == NULL)
10910Sstevel@tonic-gate 		return (ESRCH);
10920Sstevel@tonic-gate 	ulwp = *ulwpp;
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	if (ulwp->ul_dead) {
10950Sstevel@tonic-gate 		ulwp_unlock(ulwp, udp);
10960Sstevel@tonic-gate 		error = _thrp_join(tid, NULL, NULL, 0);
10970Sstevel@tonic-gate 	} else {
10980Sstevel@tonic-gate 		error = __lwp_detach(tid);
10990Sstevel@tonic-gate 		ulwp->ul_detached = 1;
11000Sstevel@tonic-gate 		ulwp->ul_usropts |= THR_DETACHED;
11010Sstevel@tonic-gate 		ulwp_unlock(ulwp, udp);
11020Sstevel@tonic-gate 	}
11030Sstevel@tonic-gate 	return (error);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate static const char *
11070Sstevel@tonic-gate ematch(const char *ev, const char *match)
11080Sstevel@tonic-gate {
11090Sstevel@tonic-gate 	int c;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	while ((c = *match++) != '\0') {
11120Sstevel@tonic-gate 		if (*ev++ != c)
11130Sstevel@tonic-gate 			return (NULL);
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 	if (*ev++ != '=')
11160Sstevel@tonic-gate 		return (NULL);
11170Sstevel@tonic-gate 	return (ev);
11180Sstevel@tonic-gate }
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate static int
11210Sstevel@tonic-gate envvar(const char *ev, const char *match, int limit)
11220Sstevel@tonic-gate {
11230Sstevel@tonic-gate 	int val = -1;
11240Sstevel@tonic-gate 	const char *ename;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	if ((ename = ematch(ev, match)) != NULL) {
11270Sstevel@tonic-gate 		int c;
11280Sstevel@tonic-gate 		for (val = 0; (c = *ename) != '\0'; ename++) {
11290Sstevel@tonic-gate 			if (!isdigit(c)) {
11300Sstevel@tonic-gate 				val = -1;
11310Sstevel@tonic-gate 				break;
11320Sstevel@tonic-gate 			}
11330Sstevel@tonic-gate 			val = val * 10 + (c - '0');
11340Sstevel@tonic-gate 			if (val > limit) {
11350Sstevel@tonic-gate 				val = limit;
11360Sstevel@tonic-gate 				break;
11370Sstevel@tonic-gate 			}
11380Sstevel@tonic-gate 		}
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 	return (val);
11410Sstevel@tonic-gate }
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate static void
11440Sstevel@tonic-gate etest(const char *ev)
11450Sstevel@tonic-gate {
11460Sstevel@tonic-gate 	int value;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	if ((value = envvar(ev, "QUEUE_SPIN", 1000000)) >= 0)
11490Sstevel@tonic-gate 		thread_queue_spin = value;
11505629Sraf 	if ((value = envvar(ev, "ADAPTIVE_SPIN", 1000000)) >= 0)
11510Sstevel@tonic-gate 		thread_adaptive_spin = value;
11525629Sraf 	if ((value = envvar(ev, "MAX_SPINNERS", 255)) >= 0)
11530Sstevel@tonic-gate 		thread_max_spinners = value;
11540Sstevel@tonic-gate 	if ((value = envvar(ev, "QUEUE_FIFO", 8)) >= 0)
11550Sstevel@tonic-gate 		thread_queue_fifo = value;
11560Sstevel@tonic-gate #if defined(THREAD_DEBUG)
11570Sstevel@tonic-gate 	if ((value = envvar(ev, "QUEUE_VERIFY", 1)) >= 0)
11580Sstevel@tonic-gate 		thread_queue_verify = value;
11590Sstevel@tonic-gate 	if ((value = envvar(ev, "QUEUE_DUMP", 1)) >= 0)
11600Sstevel@tonic-gate 		thread_queue_dump = value;
11616247Sraf #endif
11620Sstevel@tonic-gate 	if ((value = envvar(ev, "STACK_CACHE", 10000)) >= 0)
11630Sstevel@tonic-gate 		thread_stack_cache = value;
11640Sstevel@tonic-gate 	if ((value = envvar(ev, "COND_WAIT_DEFER", 1)) >= 0)
11650Sstevel@tonic-gate 		thread_cond_wait_defer = value;
11660Sstevel@tonic-gate 	if ((value = envvar(ev, "ERROR_DETECTION", 2)) >= 0)
11670Sstevel@tonic-gate 		thread_error_detection = value;
11680Sstevel@tonic-gate 	if ((value = envvar(ev, "ASYNC_SAFE", 1)) >= 0)
11690Sstevel@tonic-gate 		thread_async_safe = value;
11700Sstevel@tonic-gate 	if ((value = envvar(ev, "DOOR_NORESERVE", 1)) >= 0)
11710Sstevel@tonic-gate 		thread_door_noreserve = value;
11727255Sraf 	if ((value = envvar(ev, "LOCKS_MISALIGNED", 1)) >= 0)
11737255Sraf 		thread_locks_misaligned = value;
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate /*
11770Sstevel@tonic-gate  * Look for and evaluate environment variables of the form "_THREAD_*".
11780Sstevel@tonic-gate  * For compatibility with the past, we also look for environment
11790Sstevel@tonic-gate  * names of the form "LIBTHREAD_*".
11800Sstevel@tonic-gate  */
11810Sstevel@tonic-gate static void
11820Sstevel@tonic-gate set_thread_vars()
11830Sstevel@tonic-gate {
11846879Sraf 	extern const char **_environ;
11850Sstevel@tonic-gate 	const char **pev;
11860Sstevel@tonic-gate 	const char *ev;
11870Sstevel@tonic-gate 	char c;
11880Sstevel@tonic-gate 
11896879Sraf 	if ((pev = _environ) == NULL)
11900Sstevel@tonic-gate 		return;
11910Sstevel@tonic-gate 	while ((ev = *pev++) != NULL) {
11920Sstevel@tonic-gate 		c = *ev;
11936812Sraf 		if (c == '_' && strncmp(ev, "_THREAD_", 8) == 0)
11940Sstevel@tonic-gate 			etest(ev + 8);
11956812Sraf 		if (c == 'L' && strncmp(ev, "LIBTHREAD_", 10) == 0)
11960Sstevel@tonic-gate 			etest(ev + 10);
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate }
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate /* PROBE_SUPPORT begin */
12010Sstevel@tonic-gate #pragma weak __tnf_probe_notify
12020Sstevel@tonic-gate extern void __tnf_probe_notify(void);
12030Sstevel@tonic-gate /* PROBE_SUPPORT end */
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate /* same as atexit() but private to the library */
12060Sstevel@tonic-gate extern int _atexit(void (*)(void));
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate /* same as _cleanup() but private to the library */
12090Sstevel@tonic-gate extern void __cleanup(void);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate extern void atfork_init(void);
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate #ifdef __amd64
12146320Sbholler extern void __proc64id(void);
12150Sstevel@tonic-gate #endif
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate /*
12180Sstevel@tonic-gate  * libc_init() is called by ld.so.1 for library initialization.
12190Sstevel@tonic-gate  * We perform minimal initialization; enough to work with the main thread.
12200Sstevel@tonic-gate  */
12210Sstevel@tonic-gate void
12220Sstevel@tonic-gate libc_init(void)
12230Sstevel@tonic-gate {
12240Sstevel@tonic-gate 	uberdata_t *udp = &__uberdata;
12250Sstevel@tonic-gate 	ulwp_t *oldself = __curthread();
12260Sstevel@tonic-gate 	ucontext_t uc;
12270Sstevel@tonic-gate 	ulwp_t *self;
12280Sstevel@tonic-gate 	struct rlimit rl;
12290Sstevel@tonic-gate 	caddr_t data;
12300Sstevel@tonic-gate 	size_t tls_size;
12310Sstevel@tonic-gate 	int setmask;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	/*
12340Sstevel@tonic-gate 	 * For the initial stage of initialization, we must be careful
12350Sstevel@tonic-gate 	 * not to call any function that could possibly call _cerror().
12360Sstevel@tonic-gate 	 * For this purpose, we call only the raw system call wrappers.
12370Sstevel@tonic-gate 	 */
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate #ifdef __amd64
12400Sstevel@tonic-gate 	/*
12410Sstevel@tonic-gate 	 * Gather information about cache layouts for optimized
12426320Sbholler 	 * AMD and Intel assembler strfoo() and memfoo() functions.
12430Sstevel@tonic-gate 	 */
12446320Sbholler 	__proc64id();
12450Sstevel@tonic-gate #endif
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	/*
12480Sstevel@tonic-gate 	 * Every libc, regardless of which link map, must register __cleanup().
12490Sstevel@tonic-gate 	 */
12500Sstevel@tonic-gate 	(void) _atexit(__cleanup);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	/*
12530Sstevel@tonic-gate 	 * We keep our uberdata on one of (a) the first alternate link map
12540Sstevel@tonic-gate 	 * or (b) the primary link map.  We switch to the primary link map
12550Sstevel@tonic-gate 	 * and stay there once we see it.  All intermediate link maps are
12560Sstevel@tonic-gate 	 * subject to being unloaded at any time.
12570Sstevel@tonic-gate 	 */
12580Sstevel@tonic-gate 	if (oldself != NULL && (oldself->ul_primarymap || !primary_link_map)) {
12590Sstevel@tonic-gate 		__tdb_bootstrap = oldself->ul_uberdata->tdb_bootstrap;
12600Sstevel@tonic-gate 		mutex_setup();
12610Sstevel@tonic-gate 		atfork_init();	/* every link map needs atfork() processing */
12620Sstevel@tonic-gate 		return;
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	/*
12660Sstevel@tonic-gate 	 * To establish the main stack information, we have to get our context.
12670Sstevel@tonic-gate 	 * This is also convenient to use for getting our signal mask.
12680Sstevel@tonic-gate 	 */
12690Sstevel@tonic-gate 	uc.uc_flags = UC_ALL;
12706515Sraf 	(void) __getcontext(&uc);
12710Sstevel@tonic-gate 	ASSERT(uc.uc_link == NULL);
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
12740Sstevel@tonic-gate 	ASSERT(primary_link_map || tls_size == 0);
12750Sstevel@tonic-gate 	data = lmalloc(sizeof (ulwp_t) + tls_size);
12760Sstevel@tonic-gate 	if (data == NULL)
12770Sstevel@tonic-gate 		thr_panic("cannot allocate thread structure for main thread");
12780Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
12790Sstevel@tonic-gate 	self = (ulwp_t *)(data + tls_size);
12800Sstevel@tonic-gate 	init_hash_table[0].hash_bucket = self;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	self->ul_sigmask = uc.uc_sigmask;
12830Sstevel@tonic-gate 	delete_reserved_signals(&self->ul_sigmask);
12840Sstevel@tonic-gate 	/*
12850Sstevel@tonic-gate 	 * Are the old and new sets different?
12860Sstevel@tonic-gate 	 * (This can happen if we are currently blocking SIGCANCEL.)
12870Sstevel@tonic-gate 	 * If so, we must explicitly set our signal mask, below.
12880Sstevel@tonic-gate 	 */
12890Sstevel@tonic-gate 	setmask =
12900Sstevel@tonic-gate 	    ((self->ul_sigmask.__sigbits[0] ^ uc.uc_sigmask.__sigbits[0]) |
129111913SRoger.Faulkner@Sun.COM 	    (self->ul_sigmask.__sigbits[1] ^ uc.uc_sigmask.__sigbits[1]) |
129211913SRoger.Faulkner@Sun.COM 	    (self->ul_sigmask.__sigbits[2] ^ uc.uc_sigmask.__sigbits[2]) |
129311913SRoger.Faulkner@Sun.COM 	    (self->ul_sigmask.__sigbits[3] ^ uc.uc_sigmask.__sigbits[3]));
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate #ifdef __sparc
12960Sstevel@tonic-gate 	/*
12970Sstevel@tonic-gate 	 * We cache several instructions in the thread structure for use
12980Sstevel@tonic-gate 	 * by the fasttrap DTrace provider. When changing this, read the
12990Sstevel@tonic-gate 	 * comment in fasttrap.h for the all the other places that must
13000Sstevel@tonic-gate 	 * be changed.
13010Sstevel@tonic-gate 	 */
13020Sstevel@tonic-gate 	self->ul_dsave = 0x9de04000;	/* save %g1, %g0, %sp */
13030Sstevel@tonic-gate 	self->ul_drestore = 0x81e80000;	/* restore %g0, %g0, %g0 */
13040Sstevel@tonic-gate 	self->ul_dftret = 0x91d0203a;	/* ta 0x3a */
13050Sstevel@tonic-gate 	self->ul_dreturn = 0x81ca0000;	/* return %o0 */
13060Sstevel@tonic-gate #endif
13070Sstevel@tonic-gate 
13085891Sraf 	self->ul_stktop = (uintptr_t)uc.uc_stack.ss_sp + uc.uc_stack.ss_size;
13096515Sraf 	(void) getrlimit(RLIMIT_STACK, &rl);
13100Sstevel@tonic-gate 	self->ul_stksiz = rl.rlim_cur;
13110Sstevel@tonic-gate 	self->ul_stk = (caddr_t)(self->ul_stktop - self->ul_stksiz);
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	self->ul_forw = self->ul_back = self;
13140Sstevel@tonic-gate 	self->ul_hash = NULL;
13150Sstevel@tonic-gate 	self->ul_ix = 0;
13166812Sraf 	self->ul_lwpid = 1; /* _lwp_self() */
13170Sstevel@tonic-gate 	self->ul_main = 1;
13180Sstevel@tonic-gate 	self->ul_self = self;
13196247Sraf 	self->ul_policy = -1;		/* initialize only when needed */
13206247Sraf 	self->ul_pri = 0;
13216247Sraf 	self->ul_cid = 0;
13226296Sraf 	self->ul_rtclassid = -1;
13230Sstevel@tonic-gate 	self->ul_uberdata = udp;
13240Sstevel@tonic-gate 	if (oldself != NULL) {
13250Sstevel@tonic-gate 		int i;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 		ASSERT(primary_link_map);
13280Sstevel@tonic-gate 		ASSERT(oldself->ul_main == 1);
13290Sstevel@tonic-gate 		self->ul_stsd = oldself->ul_stsd;
13300Sstevel@tonic-gate 		for (i = 0; i < TSD_NFAST; i++)
13310Sstevel@tonic-gate 			self->ul_ftsd[i] = oldself->ul_ftsd[i];
13320Sstevel@tonic-gate 		self->ul_tls = oldself->ul_tls;
13330Sstevel@tonic-gate 		/*
13340Sstevel@tonic-gate 		 * Retrieve all pointers to uberdata allocated
13350Sstevel@tonic-gate 		 * while running on previous link maps.
13361111Sraf 		 * We would like to do a structure assignment here, but
13371111Sraf 		 * gcc turns structure assignments into calls to memcpy(),
13381111Sraf 		 * a function exported from libc.  We can't call any such
13391111Sraf 		 * external functions until we establish curthread, below,
13401111Sraf 		 * so we just call our private version of memcpy().
13410Sstevel@tonic-gate 		 */
13426515Sraf 		(void) memcpy(udp, oldself->ul_uberdata, sizeof (*udp));
13430Sstevel@tonic-gate 		/*
13440Sstevel@tonic-gate 		 * These items point to global data on the primary link map.
13450Sstevel@tonic-gate 		 */
13460Sstevel@tonic-gate 		udp->thr_hash_table = init_hash_table;
13470Sstevel@tonic-gate 		udp->sigacthandler = sigacthandler;
13480Sstevel@tonic-gate 		udp->tdb.tdb_events = tdb_events;
13490Sstevel@tonic-gate 		ASSERT(udp->nthreads == 1 && !udp->uberflags.uf_mt);
13500Sstevel@tonic-gate 		ASSERT(udp->lwp_stacks == NULL);
13510Sstevel@tonic-gate 		ASSERT(udp->ulwp_freelist == NULL);
13520Sstevel@tonic-gate 		ASSERT(udp->ulwp_replace_free == NULL);
13530Sstevel@tonic-gate 		ASSERT(udp->hash_size == 1);
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate 	udp->all_lwps = self;
13560Sstevel@tonic-gate 	udp->ulwp_one = self;
13576515Sraf 	udp->pid = getpid();
13580Sstevel@tonic-gate 	udp->nthreads = 1;
13590Sstevel@tonic-gate 	/*
13600Sstevel@tonic-gate 	 * In every link map, tdb_bootstrap points to the same piece of
13610Sstevel@tonic-gate 	 * allocated memory.  When the primary link map is initialized,
13620Sstevel@tonic-gate 	 * the allocated memory is assigned a pointer to the one true
13630Sstevel@tonic-gate 	 * uberdata.  This allows libc_db to initialize itself regardless
13640Sstevel@tonic-gate 	 * of which instance of libc it finds in the address space.
13650Sstevel@tonic-gate 	 */
13660Sstevel@tonic-gate 	if (udp->tdb_bootstrap == NULL)
13670Sstevel@tonic-gate 		udp->tdb_bootstrap = lmalloc(sizeof (uberdata_t *));
13680Sstevel@tonic-gate 	__tdb_bootstrap = udp->tdb_bootstrap;
13690Sstevel@tonic-gate 	if (primary_link_map) {
13700Sstevel@tonic-gate 		self->ul_primarymap = 1;
13710Sstevel@tonic-gate 		udp->primary_map = 1;
13720Sstevel@tonic-gate 		*udp->tdb_bootstrap = udp;
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 	/*
13750Sstevel@tonic-gate 	 * Cancellation can't happen until:
13760Sstevel@tonic-gate 	 *	pthread_cancel() is called
13770Sstevel@tonic-gate 	 * or:
13780Sstevel@tonic-gate 	 *	another thread is created
13790Sstevel@tonic-gate 	 * For now, as a single-threaded process, set the flag that tells
13800Sstevel@tonic-gate 	 * PROLOGUE/EPILOGUE (in scalls.c) that cancellation can't happen.
13810Sstevel@tonic-gate 	 */
13820Sstevel@tonic-gate 	self->ul_nocancel = 1;
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate #if defined(__amd64)
13853446Smrj 	(void) ___lwp_private(_LWP_SETPRIVATE, _LWP_FSBASE, self);
13860Sstevel@tonic-gate #elif defined(__i386)
13873446Smrj 	(void) ___lwp_private(_LWP_SETPRIVATE, _LWP_GSBASE, self);
13880Sstevel@tonic-gate #endif	/* __i386 || __amd64 */
13890Sstevel@tonic-gate 	set_curthread(self);		/* redundant on i386 */
13900Sstevel@tonic-gate 	/*
13910Sstevel@tonic-gate 	 * Now curthread is established and it is safe to call any
13920Sstevel@tonic-gate 	 * function in libc except one that uses thread-local storage.
13930Sstevel@tonic-gate 	 */
13940Sstevel@tonic-gate 	self->ul_errnop = &errno;
13950Sstevel@tonic-gate 	if (oldself != NULL) {
13960Sstevel@tonic-gate 		/* tls_size was zero when oldself was allocated */
13970Sstevel@tonic-gate 		lfree(oldself, sizeof (ulwp_t));
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate 	mutex_setup();
14000Sstevel@tonic-gate 	atfork_init();
14010Sstevel@tonic-gate 	signal_init();
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	/*
14040Sstevel@tonic-gate 	 * If the stack is unlimited, we set the size to zero to disable
14050Sstevel@tonic-gate 	 * stack checking.
14060Sstevel@tonic-gate 	 * XXX: Work harder here.  Get the stack size from /proc/self/rmap
14070Sstevel@tonic-gate 	 */
14080Sstevel@tonic-gate 	if (self->ul_stksiz == RLIM_INFINITY) {
14090Sstevel@tonic-gate 		self->ul_ustack.ss_sp = (void *)self->ul_stktop;
14100Sstevel@tonic-gate 		self->ul_ustack.ss_size = 0;
14110Sstevel@tonic-gate 	} else {
14120Sstevel@tonic-gate 		self->ul_ustack.ss_sp = self->ul_stk;
14130Sstevel@tonic-gate 		self->ul_ustack.ss_size = self->ul_stksiz;
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 	self->ul_ustack.ss_flags = 0;
14166515Sraf 	(void) setustack(&self->ul_ustack);
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	/*
14190Sstevel@tonic-gate 	 * Get the variables that affect thread behavior from the environment.
14200Sstevel@tonic-gate 	 */
14210Sstevel@tonic-gate 	set_thread_vars();
14220Sstevel@tonic-gate 	udp->uberflags.uf_thread_error_detection = (char)thread_error_detection;
14230Sstevel@tonic-gate 	udp->thread_stack_cache = thread_stack_cache;
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	/*
14260Sstevel@tonic-gate 	 * Make per-thread copies of global variables, for speed.
14270Sstevel@tonic-gate 	 */
14280Sstevel@tonic-gate 	self->ul_queue_fifo = (char)thread_queue_fifo;
14290Sstevel@tonic-gate 	self->ul_cond_wait_defer = (char)thread_cond_wait_defer;
14300Sstevel@tonic-gate 	self->ul_error_detection = (char)thread_error_detection;
14310Sstevel@tonic-gate 	self->ul_async_safe = (char)thread_async_safe;
14320Sstevel@tonic-gate 	self->ul_door_noreserve = (char)thread_door_noreserve;
14337255Sraf 	self->ul_misaligned = (char)thread_locks_misaligned;
14345629Sraf 	self->ul_max_spinners = (uint8_t)thread_max_spinners;
14350Sstevel@tonic-gate 	self->ul_adaptive_spin = thread_adaptive_spin;
14360Sstevel@tonic-gate 	self->ul_queue_spin = thread_queue_spin;
14370Sstevel@tonic-gate 
143810629SRoger.Faulkner@Sun.COM #if defined(__sparc) && !defined(_LP64)
143910629SRoger.Faulkner@Sun.COM 	if (self->ul_misaligned) {
144010629SRoger.Faulkner@Sun.COM 		/*
144110629SRoger.Faulkner@Sun.COM 		 * Tell the kernel to fix up ldx/stx instructions that
144210629SRoger.Faulkner@Sun.COM 		 * refer to non-8-byte aligned data instead of giving
144310629SRoger.Faulkner@Sun.COM 		 * the process an alignment trap and generating SIGBUS.
144410629SRoger.Faulkner@Sun.COM 		 *
144510629SRoger.Faulkner@Sun.COM 		 * Programs compiled for 32-bit sparc with the Studio SS12
144610629SRoger.Faulkner@Sun.COM 		 * compiler get this done for them automatically (in _init()).
144710629SRoger.Faulkner@Sun.COM 		 * We do it here for the benefit of programs compiled with
144810629SRoger.Faulkner@Sun.COM 		 * other compilers, like gcc.
144910629SRoger.Faulkner@Sun.COM 		 *
145010629SRoger.Faulkner@Sun.COM 		 * This is necessary for the _THREAD_LOCKS_MISALIGNED=1
145110629SRoger.Faulkner@Sun.COM 		 * environment variable horrible hack to work.
145210629SRoger.Faulkner@Sun.COM 		 */
145310629SRoger.Faulkner@Sun.COM 		extern void _do_fix_align(void);
145410629SRoger.Faulkner@Sun.COM 		_do_fix_align();
145510629SRoger.Faulkner@Sun.COM 	}
145610629SRoger.Faulkner@Sun.COM #endif
145710629SRoger.Faulkner@Sun.COM 
14580Sstevel@tonic-gate 	/*
14590Sstevel@tonic-gate 	 * When we have initialized the primary link map, inform
14600Sstevel@tonic-gate 	 * the dynamic linker about our interface functions.
14610Sstevel@tonic-gate 	 */
14620Sstevel@tonic-gate 	if (self->ul_primarymap)
14630Sstevel@tonic-gate 		_ld_libc((void *)rtld_funcs);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	/*
14660Sstevel@tonic-gate 	 * Defer signals until TLS constructors have been called.
14670Sstevel@tonic-gate 	 */
14680Sstevel@tonic-gate 	sigoff(self);
14690Sstevel@tonic-gate 	tls_setup();
14700Sstevel@tonic-gate 	sigon(self);
14710Sstevel@tonic-gate 	if (setmask)
14720Sstevel@tonic-gate 		(void) restore_signals(self);
14730Sstevel@tonic-gate 
14745891Sraf 	/*
14755891Sraf 	 * Make private copies of __xpg4 and __xpg6 so libc can test
14765891Sraf 	 * them after this point without invoking the dynamic linker.
14775891Sraf 	 */
14785891Sraf 	libc__xpg4 = __xpg4;
14795891Sraf 	libc__xpg6 = __xpg6;
14805891Sraf 
14810Sstevel@tonic-gate 	/* PROBE_SUPPORT begin */
14820Sstevel@tonic-gate 	if (self->ul_primarymap && __tnf_probe_notify != NULL)
14830Sstevel@tonic-gate 		__tnf_probe_notify();
14840Sstevel@tonic-gate 	/* PROBE_SUPPORT end */
14852248Sraf 
14862248Sraf 	init_sigev_thread();
14872248Sraf 	init_aio();
14882692Snakanon 
14892692Snakanon 	/*
14902692Snakanon 	 * We need to reset __threaded dynamically at runtime, so that
14912692Snakanon 	 * __threaded can be bound to __threaded outside libc which may not
14922692Snakanon 	 * have initial value of 1 (without a copy relocation in a.out).
14932692Snakanon 	 */
14942692Snakanon 	__threaded = 1;
14950Sstevel@tonic-gate }
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate #pragma fini(libc_fini)
14980Sstevel@tonic-gate void
14990Sstevel@tonic-gate libc_fini()
15000Sstevel@tonic-gate {
15010Sstevel@tonic-gate 	/*
15020Sstevel@tonic-gate 	 * If we are doing fini processing for the instance of libc
15030Sstevel@tonic-gate 	 * on the first alternate link map (this happens only when
15040Sstevel@tonic-gate 	 * the dynamic linker rejects a bad audit library), then clear
15050Sstevel@tonic-gate 	 * __curthread().  We abandon whatever memory was allocated by
15060Sstevel@tonic-gate 	 * lmalloc() while running on this alternate link-map but we
15070Sstevel@tonic-gate 	 * don't care (and can't find the memory in any case); we just
15080Sstevel@tonic-gate 	 * want to protect the application from this bad audit library.
15090Sstevel@tonic-gate 	 * No fini processing is done by libc in the normal case.
15100Sstevel@tonic-gate 	 */
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	if (udp->primary_map == 0 && udp == &__uberdata)
15150Sstevel@tonic-gate 		set_curthread(NULL);
15160Sstevel@tonic-gate }
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate /*
15190Sstevel@tonic-gate  * finish_init is called when we are about to become multi-threaded,
15200Sstevel@tonic-gate  * that is, on the first call to thr_create().
15210Sstevel@tonic-gate  */
15220Sstevel@tonic-gate void
15230Sstevel@tonic-gate finish_init()
15240Sstevel@tonic-gate {
15250Sstevel@tonic-gate 	ulwp_t *self = curthread;
15260Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
15270Sstevel@tonic-gate 	thr_hash_table_t *htp;
15280Sstevel@tonic-gate 	void *data;
15290Sstevel@tonic-gate 	int i;
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	/*
15320Sstevel@tonic-gate 	 * No locks needed here; we are single-threaded on the first call.
15330Sstevel@tonic-gate 	 * We can be called only after the primary link map has been set up.
15340Sstevel@tonic-gate 	 */
15350Sstevel@tonic-gate 	ASSERT(self->ul_primarymap);
15360Sstevel@tonic-gate 	ASSERT(self == udp->ulwp_one);
15370Sstevel@tonic-gate 	ASSERT(!udp->uberflags.uf_mt);
15380Sstevel@tonic-gate 	ASSERT(udp->hash_size == 1);
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	/*
15416247Sraf 	 * Initialize self->ul_policy, self->ul_cid, and self->ul_pri.
15426247Sraf 	 */
15436247Sraf 	update_sched(self);
15446247Sraf 
15456247Sraf 	/*
15466247Sraf 	 * Allocate the queue_head array if not already allocated.
15470Sstevel@tonic-gate 	 */
15480Sstevel@tonic-gate 	if (udp->queue_head == NULL)
15490Sstevel@tonic-gate 		queue_alloc();
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	/*
15520Sstevel@tonic-gate 	 * Now allocate the thread hash table.
15530Sstevel@tonic-gate 	 */
15546515Sraf 	if ((data = mmap(NULL, HASHTBLSZ * sizeof (thr_hash_table_t),
15550Sstevel@tonic-gate 	    PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, (off_t)0))
15560Sstevel@tonic-gate 	    == MAP_FAILED)
15570Sstevel@tonic-gate 		thr_panic("cannot allocate thread hash table");
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	udp->thr_hash_table = htp = (thr_hash_table_t *)data;
15600Sstevel@tonic-gate 	udp->hash_size = HASHTBLSZ;
15610Sstevel@tonic-gate 	udp->hash_mask = HASHTBLSZ - 1;
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	for (i = 0; i < HASHTBLSZ; i++, htp++) {
15644574Sraf 		htp->hash_lock.mutex_flag = LOCK_INITED;
15650Sstevel@tonic-gate 		htp->hash_lock.mutex_magic = MUTEX_MAGIC;
15660Sstevel@tonic-gate 		htp->hash_cond.cond_magic = COND_MAGIC;
15670Sstevel@tonic-gate 	}
15680Sstevel@tonic-gate 	hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	/*
15710Sstevel@tonic-gate 	 * Set up the SIGCANCEL handler for threads cancellation.
15720Sstevel@tonic-gate 	 */
15732248Sraf 	setup_cancelsig(SIGCANCEL);
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	/*
15760Sstevel@tonic-gate 	 * Arrange to do special things on exit --
15770Sstevel@tonic-gate 	 * - collect queue statistics from all remaining active threads.
15786247Sraf 	 * - dump queue statistics to stderr if _THREAD_QUEUE_DUMP is set.
15790Sstevel@tonic-gate 	 * - grab assert_lock to ensure that assertion failures
15800Sstevel@tonic-gate 	 *   and a core dump take precedence over _exit().
15810Sstevel@tonic-gate 	 * (Functions are called in the reverse order of their registration.)
15820Sstevel@tonic-gate 	 */
15836247Sraf 	(void) _atexit(grab_assert_lock);
15846247Sraf #if defined(THREAD_DEBUG)
15850Sstevel@tonic-gate 	(void) _atexit(dump_queue_statistics);
15860Sstevel@tonic-gate 	(void) _atexit(collect_queue_statistics);
15876247Sraf #endif
15880Sstevel@tonic-gate }
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate /*
15913235Sraf  * Used only by postfork1_child(), below.
15920Sstevel@tonic-gate  */
15930Sstevel@tonic-gate static void
15940Sstevel@tonic-gate mark_dead_and_buried(ulwp_t *ulwp)
15950Sstevel@tonic-gate {
15960Sstevel@tonic-gate 	ulwp->ul_dead = 1;
15970Sstevel@tonic-gate 	ulwp->ul_lwpid = (lwpid_t)(-1);
15980Sstevel@tonic-gate 	ulwp->ul_hash = NULL;
15990Sstevel@tonic-gate 	ulwp->ul_ix = -1;
16000Sstevel@tonic-gate 	ulwp->ul_schedctl = NULL;
16010Sstevel@tonic-gate 	ulwp->ul_schedctl_called = NULL;
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate /*
16050Sstevel@tonic-gate  * This is called from fork1() in the child.
16060Sstevel@tonic-gate  * Reset our data structures to reflect one lwp.
16070Sstevel@tonic-gate  */
16080Sstevel@tonic-gate void
16092248Sraf postfork1_child()
16100Sstevel@tonic-gate {
16110Sstevel@tonic-gate 	ulwp_t *self = curthread;
16120Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
16136247Sraf 	queue_head_t *qp;
16140Sstevel@tonic-gate 	ulwp_t *next;
16150Sstevel@tonic-gate 	ulwp_t *ulwp;
16160Sstevel@tonic-gate 	int i;
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	/* daemon threads shouldn't call fork1(), but oh well... */
16190Sstevel@tonic-gate 	self->ul_usropts &= ~THR_DAEMON;
16200Sstevel@tonic-gate 	udp->nthreads = 1;
16210Sstevel@tonic-gate 	udp->ndaemons = 0;
16220Sstevel@tonic-gate 	udp->uberflags.uf_mt = 0;
16231672Sraf 	__libc_threaded = 0;
16240Sstevel@tonic-gate 	for (i = 0; i < udp->hash_size; i++)
16250Sstevel@tonic-gate 		udp->thr_hash_table[i].hash_bucket = NULL;
16266812Sraf 	self->ul_lwpid = _lwp_self();
16270Sstevel@tonic-gate 	hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
16280Sstevel@tonic-gate 
16295002Sraf 	/*
16306515Sraf 	 * Some thread in the parent might have been suspended
16316515Sraf 	 * while holding udp->callout_lock or udp->ld_lock.
16326515Sraf 	 * Reinitialize the child's copies.
16335002Sraf 	 */
16346812Sraf 	(void) mutex_init(&udp->callout_lock,
16356812Sraf 	    USYNC_THREAD | LOCK_RECURSIVE, NULL);
16366812Sraf 	(void) mutex_init(&udp->ld_lock,
16376812Sraf 	    USYNC_THREAD | LOCK_RECURSIVE, NULL);
16385002Sraf 
16390Sstevel@tonic-gate 	/* no one in the child is on a sleep queue; reinitialize */
16406247Sraf 	if ((qp = udp->queue_head) != NULL) {
16416515Sraf 		(void) memset(qp, 0, 2 * QHASHSIZE * sizeof (queue_head_t));
16426247Sraf 		for (i = 0; i < 2 * QHASHSIZE; qp++, i++) {
16436247Sraf 			qp->qh_type = (i < QHASHSIZE)? MX : CV;
16446247Sraf 			qp->qh_lock.mutex_flag = LOCK_INITED;
16456247Sraf 			qp->qh_lock.mutex_magic = MUTEX_MAGIC;
16466247Sraf 			qp->qh_hlist = &qp->qh_def_root;
16476247Sraf #if defined(THREAD_DEBUG)
16486247Sraf 			qp->qh_hlen = 1;
16496247Sraf 			qp->qh_hmax = 1;
16506247Sraf #endif
16514574Sraf 		}
16520Sstevel@tonic-gate 	}
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	/*
1655*12795SRoger.Faulkner@Oracle.COM 	 * Do post-fork1 processing for subsystems that need it.
1656*12795SRoger.Faulkner@Oracle.COM 	 * We need to do this before unmapping all of the abandoned
1657*12795SRoger.Faulkner@Oracle.COM 	 * threads' stacks, below(), because the post-fork1 actions
1658*12795SRoger.Faulkner@Oracle.COM 	 * might require access to those stacks.
1659*12795SRoger.Faulkner@Oracle.COM 	 */
1660*12795SRoger.Faulkner@Oracle.COM 	postfork1_child_sigev_aio();
1661*12795SRoger.Faulkner@Oracle.COM 	postfork1_child_sigev_mq();
1662*12795SRoger.Faulkner@Oracle.COM 	postfork1_child_sigev_timer();
1663*12795SRoger.Faulkner@Oracle.COM 	postfork1_child_aio();
1664*12795SRoger.Faulkner@Oracle.COM 	/*
1665*12795SRoger.Faulkner@Oracle.COM 	 * The above subsystems use thread pools, so this action
1666*12795SRoger.Faulkner@Oracle.COM 	 * must be performed after those actions.
1667*12795SRoger.Faulkner@Oracle.COM 	 */
1668*12795SRoger.Faulkner@Oracle.COM 	postfork1_child_tpool();
1669*12795SRoger.Faulkner@Oracle.COM 
1670*12795SRoger.Faulkner@Oracle.COM 	/*
16710Sstevel@tonic-gate 	 * All lwps except ourself are gone.  Mark them so.
16720Sstevel@tonic-gate 	 * First mark all of the lwps that have already been freed.
16730Sstevel@tonic-gate 	 * Then mark and free all of the active lwps except ourself.
16740Sstevel@tonic-gate 	 * Since we are single-threaded, no locks are required here.
16750Sstevel@tonic-gate 	 */
16760Sstevel@tonic-gate 	for (ulwp = udp->lwp_stacks; ulwp != NULL; ulwp = ulwp->ul_next)
16770Sstevel@tonic-gate 		mark_dead_and_buried(ulwp);
16780Sstevel@tonic-gate 	for (ulwp = udp->ulwp_freelist; ulwp != NULL; ulwp = ulwp->ul_next)
16790Sstevel@tonic-gate 		mark_dead_and_buried(ulwp);
16800Sstevel@tonic-gate 	for (ulwp = self->ul_forw; ulwp != self; ulwp = next) {
16810Sstevel@tonic-gate 		next = ulwp->ul_forw;
16820Sstevel@tonic-gate 		ulwp->ul_forw = ulwp->ul_back = NULL;
16830Sstevel@tonic-gate 		mark_dead_and_buried(ulwp);
16840Sstevel@tonic-gate 		tsd_free(ulwp);
16850Sstevel@tonic-gate 		tls_free(ulwp);
16860Sstevel@tonic-gate 		rwl_free(ulwp);
16874574Sraf 		heldlock_free(ulwp);
16880Sstevel@tonic-gate 		ulwp_free(ulwp);
16890Sstevel@tonic-gate 	}
16900Sstevel@tonic-gate 	self->ul_forw = self->ul_back = udp->all_lwps = self;
16910Sstevel@tonic-gate 	if (self != udp->ulwp_one)
16920Sstevel@tonic-gate 		mark_dead_and_buried(udp->ulwp_one);
16930Sstevel@tonic-gate 	if ((ulwp = udp->all_zombies) != NULL) {
16940Sstevel@tonic-gate 		ASSERT(udp->nzombies != 0);
16950Sstevel@tonic-gate 		do {
16960Sstevel@tonic-gate 			next = ulwp->ul_forw;
16970Sstevel@tonic-gate 			ulwp->ul_forw = ulwp->ul_back = NULL;
16980Sstevel@tonic-gate 			mark_dead_and_buried(ulwp);
16990Sstevel@tonic-gate 			udp->nzombies--;
17000Sstevel@tonic-gate 			if (ulwp->ul_replace) {
17010Sstevel@tonic-gate 				ulwp->ul_next = NULL;
17020Sstevel@tonic-gate 				if (udp->ulwp_replace_free == NULL) {
17030Sstevel@tonic-gate 					udp->ulwp_replace_free =
17044843Sraf 					    udp->ulwp_replace_last = ulwp;
17050Sstevel@tonic-gate 				} else {
17060Sstevel@tonic-gate 					udp->ulwp_replace_last->ul_next = ulwp;
17070Sstevel@tonic-gate 					udp->ulwp_replace_last = ulwp;
17080Sstevel@tonic-gate 				}
17090Sstevel@tonic-gate 			}
17100Sstevel@tonic-gate 		} while ((ulwp = next) != udp->all_zombies);
17110Sstevel@tonic-gate 		ASSERT(udp->nzombies == 0);
17120Sstevel@tonic-gate 		udp->all_zombies = NULL;
17130Sstevel@tonic-gate 		udp->nzombies = 0;
17140Sstevel@tonic-gate 	}
17150Sstevel@tonic-gate 	trim_stack_cache(0);
17160Sstevel@tonic-gate }
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate lwpid_t
17190Sstevel@tonic-gate lwp_self(void)
17200Sstevel@tonic-gate {
17210Sstevel@tonic-gate 	return (curthread->ul_lwpid);
17220Sstevel@tonic-gate }
17230Sstevel@tonic-gate 
17246812Sraf #pragma weak _ti_thr_self = thr_self
17256812Sraf #pragma weak pthread_self = thr_self
17260Sstevel@tonic-gate thread_t
17276812Sraf thr_self()
17280Sstevel@tonic-gate {
17290Sstevel@tonic-gate 	return (curthread->ul_lwpid);
17300Sstevel@tonic-gate }
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate int
17336812Sraf thr_main()
17340Sstevel@tonic-gate {
17350Sstevel@tonic-gate 	ulwp_t *self = __curthread();
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	return ((self == NULL)? -1 : self->ul_main);
17380Sstevel@tonic-gate }
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate int
17413235Sraf _thrp_cancelled(void)
17423235Sraf {
17433235Sraf 	return (curthread->ul_rval == PTHREAD_CANCELED);
17443235Sraf }
17453235Sraf 
17463235Sraf int
17470Sstevel@tonic-gate _thrp_stksegment(ulwp_t *ulwp, stack_t *stk)
17480Sstevel@tonic-gate {
17490Sstevel@tonic-gate 	stk->ss_sp = (void *)ulwp->ul_stktop;
17500Sstevel@tonic-gate 	stk->ss_size = ulwp->ul_stksiz;
17510Sstevel@tonic-gate 	stk->ss_flags = 0;
17520Sstevel@tonic-gate 	return (0);
17530Sstevel@tonic-gate }
17540Sstevel@tonic-gate 
17556812Sraf #pragma weak _thr_stksegment = thr_stksegment
17560Sstevel@tonic-gate int
17576812Sraf thr_stksegment(stack_t *stk)
17580Sstevel@tonic-gate {
17590Sstevel@tonic-gate 	return (_thrp_stksegment(curthread, stk));
17600Sstevel@tonic-gate }
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate void
17630Sstevel@tonic-gate force_continue(ulwp_t *ulwp)
17640Sstevel@tonic-gate {
17650Sstevel@tonic-gate #if defined(THREAD_DEBUG)
17660Sstevel@tonic-gate 	ulwp_t *self = curthread;
17670Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
17680Sstevel@tonic-gate #endif
17690Sstevel@tonic-gate 	int error;
17700Sstevel@tonic-gate 	timespec_t ts;
17710Sstevel@tonic-gate 
17723512Sraf 	ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
17730Sstevel@tonic-gate 	ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	for (;;) {
17766812Sraf 		error = _lwp_continue(ulwp->ul_lwpid);
17770Sstevel@tonic-gate 		if (error != 0 && error != EINTR)
17780Sstevel@tonic-gate 			break;
17790Sstevel@tonic-gate 		error = 0;
17800Sstevel@tonic-gate 		if (ulwp->ul_stopping) {	/* he is stopping himself */
17810Sstevel@tonic-gate 			ts.tv_sec = 0;		/* give him a chance to run */
17820Sstevel@tonic-gate 			ts.tv_nsec = 100000;	/* 100 usecs or clock tick */
17832248Sraf 			(void) __nanosleep(&ts, NULL);
17840Sstevel@tonic-gate 		}
17850Sstevel@tonic-gate 		if (!ulwp->ul_stopping)		/* he is running now */
17860Sstevel@tonic-gate 			break;			/* so we are done */
17870Sstevel@tonic-gate 		/*
17880Sstevel@tonic-gate 		 * He is marked as being in the process of stopping
17890Sstevel@tonic-gate 		 * himself.  Loop around and continue him again.
17900Sstevel@tonic-gate 		 * He may not have been stopped the first time.
17910Sstevel@tonic-gate 		 */
17920Sstevel@tonic-gate 	}
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate /*
179610637SRoger.Faulkner@Sun.COM  * Suspend an lwp with lwp_suspend(), then move it to a safe point,
179710637SRoger.Faulkner@Sun.COM  * that is, to a point where ul_critical and ul_rtld are both zero.
17980Sstevel@tonic-gate  * On return, the ulwp_lock() is dropped as with ulwp_unlock().
17990Sstevel@tonic-gate  * If 'link_dropped' is non-NULL, then 'link_lock' is held on entry.
18000Sstevel@tonic-gate  * If we have to drop link_lock, we store 1 through link_dropped.
18010Sstevel@tonic-gate  * If the lwp exits before it can be suspended, we return ESRCH.
18020Sstevel@tonic-gate  */
18030Sstevel@tonic-gate int
18040Sstevel@tonic-gate safe_suspend(ulwp_t *ulwp, uchar_t whystopped, int *link_dropped)
18050Sstevel@tonic-gate {
18060Sstevel@tonic-gate 	ulwp_t *self = curthread;
18070Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
18080Sstevel@tonic-gate 	cond_t *cvp = ulwp_condvar(ulwp, udp);
18090Sstevel@tonic-gate 	mutex_t *mp = ulwp_mutex(ulwp, udp);
18100Sstevel@tonic-gate 	thread_t tid = ulwp->ul_lwpid;
18110Sstevel@tonic-gate 	int ix = ulwp->ul_ix;
18120Sstevel@tonic-gate 	int error = 0;
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	ASSERT(whystopped == TSTP_REGULAR ||
18150Sstevel@tonic-gate 	    whystopped == TSTP_MUTATOR ||
18160Sstevel@tonic-gate 	    whystopped == TSTP_FORK);
18170Sstevel@tonic-gate 	ASSERT(ulwp != self);
18180Sstevel@tonic-gate 	ASSERT(!ulwp->ul_stop);
18193512Sraf 	ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
18200Sstevel@tonic-gate 	ASSERT(MUTEX_OWNED(mp, self));
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	if (link_dropped != NULL)
18230Sstevel@tonic-gate 		*link_dropped = 0;
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	/*
18260Sstevel@tonic-gate 	 * We must grab the target's spin lock before suspending it.
18270Sstevel@tonic-gate 	 * See the comments below and in _thrp_suspend() for why.
18280Sstevel@tonic-gate 	 */
18290Sstevel@tonic-gate 	spin_lock_set(&ulwp->ul_spinlock);
18300Sstevel@tonic-gate 	(void) ___lwp_suspend(tid);
18310Sstevel@tonic-gate 	spin_lock_clear(&ulwp->ul_spinlock);
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate top:
183410637SRoger.Faulkner@Sun.COM 	if ((ulwp->ul_critical == 0 && ulwp->ul_rtld == 0) ||
183510637SRoger.Faulkner@Sun.COM 	    ulwp->ul_stopping) {
18360Sstevel@tonic-gate 		/* thread is already safe */
18370Sstevel@tonic-gate 		ulwp->ul_stop |= whystopped;
18380Sstevel@tonic-gate 	} else {
18390Sstevel@tonic-gate 		/*
18400Sstevel@tonic-gate 		 * Setting ul_pleasestop causes the target thread to stop
18410Sstevel@tonic-gate 		 * itself in _thrp_suspend(), below, after we drop its lock.
18420Sstevel@tonic-gate 		 * We must continue the critical thread before dropping
18430Sstevel@tonic-gate 		 * link_lock because the critical thread may be holding
18440Sstevel@tonic-gate 		 * the queue lock for link_lock.  This is delicate.
18450Sstevel@tonic-gate 		 */
18460Sstevel@tonic-gate 		ulwp->ul_pleasestop |= whystopped;
18470Sstevel@tonic-gate 		force_continue(ulwp);
18480Sstevel@tonic-gate 		if (link_dropped != NULL) {
18490Sstevel@tonic-gate 			*link_dropped = 1;
18500Sstevel@tonic-gate 			lmutex_unlock(&udp->link_lock);
18510Sstevel@tonic-gate 			/* be sure to drop link_lock only once */
18520Sstevel@tonic-gate 			link_dropped = NULL;
18530Sstevel@tonic-gate 		}
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 		/*
18560Sstevel@tonic-gate 		 * The thread may disappear by calling thr_exit() so we
18570Sstevel@tonic-gate 		 * cannot rely on the ulwp pointer after dropping the lock.
18580Sstevel@tonic-gate 		 * Instead, we search the hash table to find it again.
18590Sstevel@tonic-gate 		 * When we return, we may find that the thread has been
18600Sstevel@tonic-gate 		 * continued by some other thread.  The suspend/continue
18610Sstevel@tonic-gate 		 * interfaces are prone to such race conditions by design.
18620Sstevel@tonic-gate 		 */
18630Sstevel@tonic-gate 		while (ulwp && !ulwp->ul_dead && !ulwp->ul_stop &&
18640Sstevel@tonic-gate 		    (ulwp->ul_pleasestop & whystopped)) {
18655891Sraf 			(void) __cond_wait(cvp, mp);
18660Sstevel@tonic-gate 			for (ulwp = udp->thr_hash_table[ix].hash_bucket;
18670Sstevel@tonic-gate 			    ulwp != NULL; ulwp = ulwp->ul_hash) {
18680Sstevel@tonic-gate 				if (ulwp->ul_lwpid == tid)
18690Sstevel@tonic-gate 					break;
18700Sstevel@tonic-gate 			}
18710Sstevel@tonic-gate 		}
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 		if (ulwp == NULL || ulwp->ul_dead)
18740Sstevel@tonic-gate 			error = ESRCH;
18750Sstevel@tonic-gate 		else {
18760Sstevel@tonic-gate 			/*
18770Sstevel@tonic-gate 			 * Do another lwp_suspend() to make sure we don't
18780Sstevel@tonic-gate 			 * return until the target thread is fully stopped
18790Sstevel@tonic-gate 			 * in the kernel.  Don't apply lwp_suspend() until
18800Sstevel@tonic-gate 			 * we know that the target is not holding any
18810Sstevel@tonic-gate 			 * queue locks, that is, that it has completed
18820Sstevel@tonic-gate 			 * ulwp_unlock(self) and has, or at least is
18830Sstevel@tonic-gate 			 * about to, call lwp_suspend() on itself.  We do
18840Sstevel@tonic-gate 			 * this by grabbing the target's spin lock.
18850Sstevel@tonic-gate 			 */
18860Sstevel@tonic-gate 			ASSERT(ulwp->ul_lwpid == tid);
18870Sstevel@tonic-gate 			spin_lock_set(&ulwp->ul_spinlock);
18880Sstevel@tonic-gate 			(void) ___lwp_suspend(tid);
18890Sstevel@tonic-gate 			spin_lock_clear(&ulwp->ul_spinlock);
18900Sstevel@tonic-gate 			/*
18910Sstevel@tonic-gate 			 * If some other thread did a thr_continue()
18920Sstevel@tonic-gate 			 * on the target thread we have to start over.
18930Sstevel@tonic-gate 			 */
18940Sstevel@tonic-gate 			if (!ulwp->ul_stopping || !(ulwp->ul_stop & whystopped))
18950Sstevel@tonic-gate 				goto top;
18960Sstevel@tonic-gate 		}
18970Sstevel@tonic-gate 	}
18980Sstevel@tonic-gate 
18996812Sraf 	(void) cond_broadcast(cvp);
19000Sstevel@tonic-gate 	lmutex_unlock(mp);
19010Sstevel@tonic-gate 	return (error);
19020Sstevel@tonic-gate }
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate int
19050Sstevel@tonic-gate _thrp_suspend(thread_t tid, uchar_t whystopped)
19060Sstevel@tonic-gate {
19070Sstevel@tonic-gate 	ulwp_t *self = curthread;
19080Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
19090Sstevel@tonic-gate 	ulwp_t *ulwp;
19100Sstevel@tonic-gate 	int error = 0;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	ASSERT((whystopped & (TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) != 0);
19130Sstevel@tonic-gate 	ASSERT((whystopped & ~(TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) == 0);
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	/*
19164561Sraf 	 * We can't suspend anyone except ourself while
19174561Sraf 	 * some other thread is performing a fork.
19184561Sraf 	 * This also allows only one suspension at a time.
19190Sstevel@tonic-gate 	 */
19204561Sraf 	if (tid != self->ul_lwpid)
19214843Sraf 		fork_lock_enter();
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	if ((ulwp = find_lwp(tid)) == NULL)
19240Sstevel@tonic-gate 		error = ESRCH;
19250Sstevel@tonic-gate 	else if (whystopped == TSTP_MUTATOR && !ulwp->ul_mutator) {
19260Sstevel@tonic-gate 		ulwp_unlock(ulwp, udp);
19270Sstevel@tonic-gate 		error = EINVAL;
19280Sstevel@tonic-gate 	} else if (ulwp->ul_stop) {	/* already stopped */
19290Sstevel@tonic-gate 		ulwp->ul_stop |= whystopped;
19300Sstevel@tonic-gate 		ulwp_broadcast(ulwp);
19310Sstevel@tonic-gate 		ulwp_unlock(ulwp, udp);
19320Sstevel@tonic-gate 	} else if (ulwp != self) {
19330Sstevel@tonic-gate 		/*
19340Sstevel@tonic-gate 		 * After suspending the other thread, move it out of a
19350Sstevel@tonic-gate 		 * critical section and deal with the schedctl mappings.
19360Sstevel@tonic-gate 		 * safe_suspend() suspends the other thread, calls
19370Sstevel@tonic-gate 		 * ulwp_broadcast(ulwp) and drops the ulwp lock.
19380Sstevel@tonic-gate 		 */
19390Sstevel@tonic-gate 		error = safe_suspend(ulwp, whystopped, NULL);
19400Sstevel@tonic-gate 	} else {
19410Sstevel@tonic-gate 		int schedctl_after_fork = 0;
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 		/*
19440Sstevel@tonic-gate 		 * We are suspending ourself.  We must not take a signal
19450Sstevel@tonic-gate 		 * until we return from lwp_suspend() and clear ul_stopping.
19460Sstevel@tonic-gate 		 * This is to guard against siglongjmp().
19470Sstevel@tonic-gate 		 */
19480Sstevel@tonic-gate 		enter_critical(self);
19490Sstevel@tonic-gate 		self->ul_sp = stkptr();
19500Sstevel@tonic-gate 		_flush_windows();	/* sparc */
19510Sstevel@tonic-gate 		self->ul_pleasestop = 0;
19520Sstevel@tonic-gate 		self->ul_stop |= whystopped;
19530Sstevel@tonic-gate 		/*
19540Sstevel@tonic-gate 		 * Grab our spin lock before dropping ulwp_mutex(self).
19550Sstevel@tonic-gate 		 * This prevents the suspending thread from applying
19560Sstevel@tonic-gate 		 * lwp_suspend() to us before we emerge from
19570Sstevel@tonic-gate 		 * lmutex_unlock(mp) and have dropped mp's queue lock.
19580Sstevel@tonic-gate 		 */
19590Sstevel@tonic-gate 		spin_lock_set(&self->ul_spinlock);
19600Sstevel@tonic-gate 		self->ul_stopping = 1;
19610Sstevel@tonic-gate 		ulwp_broadcast(self);
19620Sstevel@tonic-gate 		ulwp_unlock(self, udp);
19630Sstevel@tonic-gate 		/*
19640Sstevel@tonic-gate 		 * From this point until we return from lwp_suspend(),
19650Sstevel@tonic-gate 		 * we must not call any function that might invoke the
19660Sstevel@tonic-gate 		 * dynamic linker, that is, we can only call functions
19670Sstevel@tonic-gate 		 * private to the library.
19680Sstevel@tonic-gate 		 *
19690Sstevel@tonic-gate 		 * Also, this is a nasty race condition for a process
19700Sstevel@tonic-gate 		 * that is undergoing a forkall() operation:
19710Sstevel@tonic-gate 		 * Once we clear our spinlock (below), we are vulnerable
19720Sstevel@tonic-gate 		 * to being suspended by the forkall() thread before
19730Sstevel@tonic-gate 		 * we manage to suspend ourself in ___lwp_suspend().
19740Sstevel@tonic-gate 		 * See safe_suspend() and force_continue().
19750Sstevel@tonic-gate 		 *
19760Sstevel@tonic-gate 		 * To avoid a SIGSEGV due to the disappearance
19770Sstevel@tonic-gate 		 * of the schedctl mappings in the child process,
19780Sstevel@tonic-gate 		 * which can happen in spin_lock_clear() if we
19790Sstevel@tonic-gate 		 * are suspended while we are in the middle of
19800Sstevel@tonic-gate 		 * its call to preempt(), we preemptively clear
19810Sstevel@tonic-gate 		 * our own schedctl pointer before dropping our
19820Sstevel@tonic-gate 		 * spinlock.  We reinstate it, in both the parent
19830Sstevel@tonic-gate 		 * and (if this really is a forkall()) the child.
19840Sstevel@tonic-gate 		 */
19850Sstevel@tonic-gate 		if (whystopped & TSTP_FORK) {
19860Sstevel@tonic-gate 			schedctl_after_fork = 1;
19870Sstevel@tonic-gate 			self->ul_schedctl = NULL;
19880Sstevel@tonic-gate 			self->ul_schedctl_called = &udp->uberflags;
19890Sstevel@tonic-gate 		}
19900Sstevel@tonic-gate 		spin_lock_clear(&self->ul_spinlock);
19910Sstevel@tonic-gate 		(void) ___lwp_suspend(tid);
19920Sstevel@tonic-gate 		/*
19930Sstevel@tonic-gate 		 * Somebody else continued us.
19940Sstevel@tonic-gate 		 * We can't grab ulwp_lock(self)
19950Sstevel@tonic-gate 		 * until after clearing ul_stopping.
19960Sstevel@tonic-gate 		 * force_continue() relies on this.
19970Sstevel@tonic-gate 		 */
19980Sstevel@tonic-gate 		self->ul_stopping = 0;
19990Sstevel@tonic-gate 		self->ul_sp = 0;
20000Sstevel@tonic-gate 		if (schedctl_after_fork) {
20010Sstevel@tonic-gate 			self->ul_schedctl_called = NULL;
20020Sstevel@tonic-gate 			self->ul_schedctl = NULL;
20030Sstevel@tonic-gate 			(void) setup_schedctl();
20040Sstevel@tonic-gate 		}
20050Sstevel@tonic-gate 		ulwp_lock(self, udp);
20060Sstevel@tonic-gate 		ulwp_broadcast(self);
20070Sstevel@tonic-gate 		ulwp_unlock(self, udp);
20080Sstevel@tonic-gate 		exit_critical(self);
20090Sstevel@tonic-gate 	}
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	if (tid != self->ul_lwpid)
20120Sstevel@tonic-gate 		fork_lock_exit();
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 	return (error);
20150Sstevel@tonic-gate }
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate /*
20180Sstevel@tonic-gate  * Suspend all lwps other than ourself in preparation for fork.
20190Sstevel@tonic-gate  */
20200Sstevel@tonic-gate void
20210Sstevel@tonic-gate suspend_fork()
20220Sstevel@tonic-gate {
20230Sstevel@tonic-gate 	ulwp_t *self = curthread;
20240Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
20250Sstevel@tonic-gate 	ulwp_t *ulwp;
20260Sstevel@tonic-gate 	int link_dropped;
20270Sstevel@tonic-gate 
20283512Sraf 	ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
20290Sstevel@tonic-gate top:
20300Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 	for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
20330Sstevel@tonic-gate 		ulwp_lock(ulwp, udp);
20340Sstevel@tonic-gate 		if (ulwp->ul_stop) {	/* already stopped */
20350Sstevel@tonic-gate 			ulwp->ul_stop |= TSTP_FORK;
20360Sstevel@tonic-gate 			ulwp_broadcast(ulwp);
20370Sstevel@tonic-gate 			ulwp_unlock(ulwp, udp);
20380Sstevel@tonic-gate 		} else {
20390Sstevel@tonic-gate 			/*
20400Sstevel@tonic-gate 			 * Move the stopped lwp out of a critical section.
20410Sstevel@tonic-gate 			 */
20420Sstevel@tonic-gate 			if (safe_suspend(ulwp, TSTP_FORK, &link_dropped) ||
20430Sstevel@tonic-gate 			    link_dropped)
20440Sstevel@tonic-gate 				goto top;
20450Sstevel@tonic-gate 		}
20460Sstevel@tonic-gate 	}
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	lmutex_unlock(&udp->link_lock);
20490Sstevel@tonic-gate }
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate void
20520Sstevel@tonic-gate continue_fork(int child)
20530Sstevel@tonic-gate {
20540Sstevel@tonic-gate 	ulwp_t *self = curthread;
20550Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
20560Sstevel@tonic-gate 	ulwp_t *ulwp;
20570Sstevel@tonic-gate 
20583512Sraf 	ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
20593512Sraf 
20600Sstevel@tonic-gate 	/*
20610Sstevel@tonic-gate 	 * Clear the schedctl pointers in the child of forkall().
20620Sstevel@tonic-gate 	 */
20630Sstevel@tonic-gate 	if (child) {
20640Sstevel@tonic-gate 		for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
20650Sstevel@tonic-gate 			ulwp->ul_schedctl_called =
20664843Sraf 			    ulwp->ul_dead? &udp->uberflags : NULL;
20670Sstevel@tonic-gate 			ulwp->ul_schedctl = NULL;
20680Sstevel@tonic-gate 		}
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	/*
20720Sstevel@tonic-gate 	 * Set all lwps that were stopped for fork() running again.
20730Sstevel@tonic-gate 	 */
20740Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
20750Sstevel@tonic-gate 	for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
20760Sstevel@tonic-gate 		mutex_t *mp = ulwp_mutex(ulwp, udp);
20770Sstevel@tonic-gate 		lmutex_lock(mp);
20780Sstevel@tonic-gate 		ASSERT(ulwp->ul_stop & TSTP_FORK);
20790Sstevel@tonic-gate 		ulwp->ul_stop &= ~TSTP_FORK;
20800Sstevel@tonic-gate 		ulwp_broadcast(ulwp);
20810Sstevel@tonic-gate 		if (!ulwp->ul_stop)
20820Sstevel@tonic-gate 			force_continue(ulwp);
20830Sstevel@tonic-gate 		lmutex_unlock(mp);
20840Sstevel@tonic-gate 	}
20850Sstevel@tonic-gate 	lmutex_unlock(&udp->link_lock);
20860Sstevel@tonic-gate }
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate int
20890Sstevel@tonic-gate _thrp_continue(thread_t tid, uchar_t whystopped)
20900Sstevel@tonic-gate {
20910Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
20920Sstevel@tonic-gate 	ulwp_t *ulwp;
20930Sstevel@tonic-gate 	mutex_t *mp;
20940Sstevel@tonic-gate 	int error = 0;
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate 	ASSERT(whystopped == TSTP_REGULAR ||
20970Sstevel@tonic-gate 	    whystopped == TSTP_MUTATOR);
20980Sstevel@tonic-gate 
20993512Sraf 	/*
21003512Sraf 	 * We single-thread the entire thread suspend/continue mechanism.
21013512Sraf 	 */
21024843Sraf 	fork_lock_enter();
21033512Sraf 
21043512Sraf 	if ((ulwp = find_lwp(tid)) == NULL) {
21053512Sraf 		fork_lock_exit();
21060Sstevel@tonic-gate 		return (ESRCH);
21073512Sraf 	}
21080Sstevel@tonic-gate 
21090Sstevel@tonic-gate 	mp = ulwp_mutex(ulwp, udp);
21100Sstevel@tonic-gate 	if ((whystopped == TSTP_MUTATOR && !ulwp->ul_mutator)) {
21110Sstevel@tonic-gate 		error = EINVAL;
21120Sstevel@tonic-gate 	} else if (ulwp->ul_stop & whystopped) {
21130Sstevel@tonic-gate 		ulwp->ul_stop &= ~whystopped;
21140Sstevel@tonic-gate 		ulwp_broadcast(ulwp);
21150Sstevel@tonic-gate 		if (!ulwp->ul_stop) {
21160Sstevel@tonic-gate 			if (whystopped == TSTP_REGULAR && ulwp->ul_created) {
21170Sstevel@tonic-gate 				ulwp->ul_sp = 0;
21180Sstevel@tonic-gate 				ulwp->ul_created = 0;
21190Sstevel@tonic-gate 			}
21200Sstevel@tonic-gate 			force_continue(ulwp);
21210Sstevel@tonic-gate 		}
21220Sstevel@tonic-gate 	}
21233512Sraf 	lmutex_unlock(mp);
21240Sstevel@tonic-gate 
21253512Sraf 	fork_lock_exit();
21260Sstevel@tonic-gate 	return (error);
21270Sstevel@tonic-gate }
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate int
21306812Sraf thr_suspend(thread_t tid)
21310Sstevel@tonic-gate {
21320Sstevel@tonic-gate 	return (_thrp_suspend(tid, TSTP_REGULAR));
21330Sstevel@tonic-gate }
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate int
21366812Sraf thr_continue(thread_t tid)
21370Sstevel@tonic-gate {
21380Sstevel@tonic-gate 	return (_thrp_continue(tid, TSTP_REGULAR));
21390Sstevel@tonic-gate }
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate void
21426812Sraf thr_yield()
21430Sstevel@tonic-gate {
21446515Sraf 	yield();
21450Sstevel@tonic-gate }
21460Sstevel@tonic-gate 
21476812Sraf #pragma weak pthread_kill = thr_kill
21486812Sraf #pragma weak _thr_kill = thr_kill
21490Sstevel@tonic-gate int
21506812Sraf thr_kill(thread_t tid, int sig)
21510Sstevel@tonic-gate {
21520Sstevel@tonic-gate 	if (sig == SIGCANCEL)
21530Sstevel@tonic-gate 		return (EINVAL);
21546812Sraf 	return (_lwp_kill(tid, sig));
21550Sstevel@tonic-gate }
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate /*
21580Sstevel@tonic-gate  * Exit a critical section, take deferred actions if necessary.
215910637SRoger.Faulkner@Sun.COM  * Called from exit_critical() and from sigon().
21600Sstevel@tonic-gate  */
21610Sstevel@tonic-gate void
21620Sstevel@tonic-gate do_exit_critical()
21630Sstevel@tonic-gate {
21640Sstevel@tonic-gate 	ulwp_t *self = curthread;
21650Sstevel@tonic-gate 	int sig;
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
216810637SRoger.Faulkner@Sun.COM 
216910637SRoger.Faulkner@Sun.COM 	/*
217010637SRoger.Faulkner@Sun.COM 	 * Don't suspend ourself or take a deferred signal while dying
217110637SRoger.Faulkner@Sun.COM 	 * or while executing inside the dynamic linker (ld.so.1).
217210637SRoger.Faulkner@Sun.COM 	 */
217310637SRoger.Faulkner@Sun.COM 	if (self->ul_dead || self->ul_rtld)
21740Sstevel@tonic-gate 		return;
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 	while (self->ul_pleasestop ||
21770Sstevel@tonic-gate 	    (self->ul_cursig != 0 && self->ul_sigdefer == 0)) {
21780Sstevel@tonic-gate 		/*
21790Sstevel@tonic-gate 		 * Avoid a recursive call to exit_critical() in _thrp_suspend()
21800Sstevel@tonic-gate 		 * by keeping self->ul_critical == 1 here.
21810Sstevel@tonic-gate 		 */
21820Sstevel@tonic-gate 		self->ul_critical++;
21830Sstevel@tonic-gate 		while (self->ul_pleasestop) {
21840Sstevel@tonic-gate 			/*
21850Sstevel@tonic-gate 			 * Guard against suspending ourself while on a sleep
21860Sstevel@tonic-gate 			 * queue.  See the comments in call_user_handler().
21870Sstevel@tonic-gate 			 */
21880Sstevel@tonic-gate 			unsleep_self();
21890Sstevel@tonic-gate 			set_parking_flag(self, 0);
21900Sstevel@tonic-gate 			(void) _thrp_suspend(self->ul_lwpid,
21914843Sraf 			    self->ul_pleasestop);
21920Sstevel@tonic-gate 		}
21930Sstevel@tonic-gate 		self->ul_critical--;
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate 		if ((sig = self->ul_cursig) != 0 && self->ul_sigdefer == 0) {
21960Sstevel@tonic-gate 			/*
21970Sstevel@tonic-gate 			 * Clear ul_cursig before proceeding.
21980Sstevel@tonic-gate 			 * This protects us from the dynamic linker's
21990Sstevel@tonic-gate 			 * calls to bind_guard()/bind_clear() in the
22000Sstevel@tonic-gate 			 * event that it is invoked to resolve a symbol
22010Sstevel@tonic-gate 			 * like take_deferred_signal() below.
22020Sstevel@tonic-gate 			 */
22030Sstevel@tonic-gate 			self->ul_cursig = 0;
22040Sstevel@tonic-gate 			take_deferred_signal(sig);
22050Sstevel@tonic-gate 			ASSERT(self->ul_cursig == 0);
22060Sstevel@tonic-gate 		}
22070Sstevel@tonic-gate 	}
22080Sstevel@tonic-gate 	ASSERT(self->ul_critical == 0);
22090Sstevel@tonic-gate }
22100Sstevel@tonic-gate 
22115891Sraf /*
22125891Sraf  * _ti_bind_guard() and _ti_bind_clear() are called by the dynamic linker
22135891Sraf  * (ld.so.1) when it has do do something, like resolve a symbol to be called
22145891Sraf  * by the application or one of its libraries.  _ti_bind_guard() is called
22155891Sraf  * on entry to ld.so.1, _ti_bind_clear() on exit from ld.so.1 back to the
22165891Sraf  * application.  The dynamic linker gets special dispensation from libc to
22175891Sraf  * run in a critical region (all signals deferred and no thread suspension
22185891Sraf  * or forking allowed), and to be immune from cancellation for the duration.
22195891Sraf  */
22200Sstevel@tonic-gate int
22216515Sraf _ti_bind_guard(int flags)
22220Sstevel@tonic-gate {
22230Sstevel@tonic-gate 	ulwp_t *self = curthread;
22246515Sraf 	uberdata_t *udp = self->ul_uberdata;
22256515Sraf 	int bindflag = (flags & THR_FLG_RTLD);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	if ((self->ul_bindflags & bindflag) == bindflag)
22280Sstevel@tonic-gate 		return (0);
22299569SRod.Evans@Sun.COM 	self->ul_bindflags |= bindflag;
22306515Sraf 	if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
22316515Sraf 		sigoff(self);	/* see no signals while holding ld_lock */
223210637SRoger.Faulkner@Sun.COM 		self->ul_rtld++;	/* don't suspend while in ld.so.1 */
22336812Sraf 		(void) mutex_lock(&udp->ld_lock);
22346515Sraf 	}
22350Sstevel@tonic-gate 	enter_critical(self);
22365891Sraf 	self->ul_save_state = self->ul_cancel_disabled;
22375891Sraf 	self->ul_cancel_disabled = 1;
22385891Sraf 	set_cancel_pending_flag(self, 0);
22390Sstevel@tonic-gate 	return (1);
22400Sstevel@tonic-gate }
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate int
22436515Sraf _ti_bind_clear(int flags)
22440Sstevel@tonic-gate {
22450Sstevel@tonic-gate 	ulwp_t *self = curthread;
22466515Sraf 	uberdata_t *udp = self->ul_uberdata;
22476515Sraf 	int bindflag = (flags & THR_FLG_RTLD);
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate 	if ((self->ul_bindflags & bindflag) == 0)
22500Sstevel@tonic-gate 		return (self->ul_bindflags);
22510Sstevel@tonic-gate 	self->ul_bindflags &= ~bindflag;
22525891Sraf 	self->ul_cancel_disabled = self->ul_save_state;
22535891Sraf 	set_cancel_pending_flag(self, 0);
22540Sstevel@tonic-gate 	exit_critical(self);
22556515Sraf 	if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
22566515Sraf 		if (MUTEX_OWNED(&udp->ld_lock, self)) {
22576812Sraf 			(void) mutex_unlock(&udp->ld_lock);
225810637SRoger.Faulkner@Sun.COM 			self->ul_rtld--;
22596515Sraf 			sigon(self);	/* reenable signals */
22606515Sraf 		}
22616515Sraf 	}
22620Sstevel@tonic-gate 	return (self->ul_bindflags);
22630Sstevel@tonic-gate }
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate /*
22669569SRod.Evans@Sun.COM  * Tell the dynamic linker (ld.so.1) whether or not it was entered from
22679569SRod.Evans@Sun.COM  * a critical region in libc.  Return zero if not, else return non-zero.
22689569SRod.Evans@Sun.COM  */
22699569SRod.Evans@Sun.COM int
22709569SRod.Evans@Sun.COM _ti_critical(void)
22719569SRod.Evans@Sun.COM {
22729569SRod.Evans@Sun.COM 	ulwp_t *self = curthread;
22739569SRod.Evans@Sun.COM 	int level = self->ul_critical;
22749569SRod.Evans@Sun.COM 
22759569SRod.Evans@Sun.COM 	if ((self->ul_bindflags & THR_FLG_RTLD) == 0 || level == 0)
22769569SRod.Evans@Sun.COM 		return (level);	/* ld.so.1 hasn't (yet) called enter() */
22779569SRod.Evans@Sun.COM 	return (level - 1);
22789569SRod.Evans@Sun.COM }
22799569SRod.Evans@Sun.COM 
22809569SRod.Evans@Sun.COM /*
22810Sstevel@tonic-gate  * sigoff() and sigon() enable cond_wait() to behave (optionally) like
22820Sstevel@tonic-gate  * it does in the old libthread (see the comments in cond_wait_queue()).
22830Sstevel@tonic-gate  * Also, signals are deferred at thread startup until TLS constructors
22846812Sraf  * have all been called, at which time _thrp_setup() calls sigon().
22851885Sraf  *
22862248Sraf  * _sigoff() and _sigon() are external consolidation-private interfaces to
22872248Sraf  * sigoff() and sigon(), respectively, in libc.  These are used in libnsl.
22880Sstevel@tonic-gate  * Also, _sigoff() and _sigon() are called from dbx's run-time checking
22890Sstevel@tonic-gate  * (librtc.so) to defer signals during its critical sections (not to be
22900Sstevel@tonic-gate  * confused with libc critical sections [see exit_critical() above]).
22910Sstevel@tonic-gate  */
22920Sstevel@tonic-gate void
22930Sstevel@tonic-gate _sigoff(void)
22940Sstevel@tonic-gate {
229510637SRoger.Faulkner@Sun.COM 	ulwp_t *self = curthread;
229610637SRoger.Faulkner@Sun.COM 
229710637SRoger.Faulkner@Sun.COM 	sigoff(self);
22980Sstevel@tonic-gate }
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate void
23010Sstevel@tonic-gate _sigon(void)
23020Sstevel@tonic-gate {
230310637SRoger.Faulkner@Sun.COM 	ulwp_t *self = curthread;
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 	ASSERT(self->ul_sigdefer > 0);
230610637SRoger.Faulkner@Sun.COM 	sigon(self);
23070Sstevel@tonic-gate }
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate int
23106812Sraf thr_getconcurrency()
23110Sstevel@tonic-gate {
23120Sstevel@tonic-gate 	return (thr_concurrency);
23130Sstevel@tonic-gate }
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate int
23166812Sraf pthread_getconcurrency()
23170Sstevel@tonic-gate {
23180Sstevel@tonic-gate 	return (pthread_concurrency);
23190Sstevel@tonic-gate }
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate int
23226812Sraf thr_setconcurrency(int new_level)
23230Sstevel@tonic-gate {
23240Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate 	if (new_level < 0)
23270Sstevel@tonic-gate 		return (EINVAL);
23280Sstevel@tonic-gate 	if (new_level > 65536)		/* 65536 is totally arbitrary */
23290Sstevel@tonic-gate 		return (EAGAIN);
23300Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
23310Sstevel@tonic-gate 	if (new_level > thr_concurrency)
23320Sstevel@tonic-gate 		thr_concurrency = new_level;
23330Sstevel@tonic-gate 	lmutex_unlock(&udp->link_lock);
23340Sstevel@tonic-gate 	return (0);
23350Sstevel@tonic-gate }
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate int
23386812Sraf pthread_setconcurrency(int new_level)
23390Sstevel@tonic-gate {
23400Sstevel@tonic-gate 	if (new_level < 0)
23410Sstevel@tonic-gate 		return (EINVAL);
23420Sstevel@tonic-gate 	if (new_level > 65536)		/* 65536 is totally arbitrary */
23430Sstevel@tonic-gate 		return (EAGAIN);
23440Sstevel@tonic-gate 	pthread_concurrency = new_level;
23450Sstevel@tonic-gate 	return (0);
23460Sstevel@tonic-gate }
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate size_t
23496812Sraf thr_min_stack(void)
23500Sstevel@tonic-gate {
23510Sstevel@tonic-gate 	return (MINSTACK);
23520Sstevel@tonic-gate }
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate int
23550Sstevel@tonic-gate __nthreads(void)
23560Sstevel@tonic-gate {
23570Sstevel@tonic-gate 	return (curthread->ul_uberdata->nthreads);
23580Sstevel@tonic-gate }
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate /*
23610Sstevel@tonic-gate  * XXX
23620Sstevel@tonic-gate  * The remainder of this file implements the private interfaces to java for
23630Sstevel@tonic-gate  * garbage collection.  It is no longer used, at least by java 1.2.
23640Sstevel@tonic-gate  * It can all go away once all old JVMs have disappeared.
23650Sstevel@tonic-gate  */
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate int	suspendingallmutators;	/* when non-zero, suspending all mutators. */
23680Sstevel@tonic-gate int	suspendedallmutators;	/* when non-zero, all mutators suspended. */
23690Sstevel@tonic-gate int	mutatorsbarrier;	/* when non-zero, mutators barrier imposed. */
23700Sstevel@tonic-gate mutex_t	mutatorslock = DEFAULTMUTEX;	/* used to enforce mutators barrier. */
23710Sstevel@tonic-gate cond_t	mutatorscv = DEFAULTCV;		/* where non-mutators sleep. */
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate /*
23740Sstevel@tonic-gate  * Get the available register state for the target thread.
23750Sstevel@tonic-gate  * Return non-volatile registers: TRS_NONVOLATILE
23760Sstevel@tonic-gate  */
23776812Sraf #pragma weak _thr_getstate = thr_getstate
23780Sstevel@tonic-gate int
23796812Sraf thr_getstate(thread_t tid, int *flag, lwpid_t *lwp, stack_t *ss, gregset_t rs)
23800Sstevel@tonic-gate {
23810Sstevel@tonic-gate 	ulwp_t *self = curthread;
23820Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
23830Sstevel@tonic-gate 	ulwp_t **ulwpp;
23840Sstevel@tonic-gate 	ulwp_t *ulwp;
23850Sstevel@tonic-gate 	int error = 0;
23860Sstevel@tonic-gate 	int trs_flag = TRS_LWPID;
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 	if (tid == 0 || self->ul_lwpid == tid) {
23890Sstevel@tonic-gate 		ulwp = self;
23900Sstevel@tonic-gate 		ulwp_lock(ulwp, udp);
23910Sstevel@tonic-gate 	} else if ((ulwpp = find_lwpp(tid)) != NULL) {
23920Sstevel@tonic-gate 		ulwp = *ulwpp;
23930Sstevel@tonic-gate 	} else {
23940Sstevel@tonic-gate 		if (flag)
23950Sstevel@tonic-gate 			*flag = TRS_INVALID;
23960Sstevel@tonic-gate 		return (ESRCH);
23970Sstevel@tonic-gate 	}
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	if (ulwp->ul_dead) {
24000Sstevel@tonic-gate 		trs_flag = TRS_INVALID;
24010Sstevel@tonic-gate 	} else if (!ulwp->ul_stop && !suspendedallmutators) {
24020Sstevel@tonic-gate 		error = EINVAL;
24030Sstevel@tonic-gate 		trs_flag = TRS_INVALID;
24040Sstevel@tonic-gate 	} else if (ulwp->ul_stop) {
24050Sstevel@tonic-gate 		trs_flag = TRS_NONVOLATILE;
24060Sstevel@tonic-gate 		getgregs(ulwp, rs);
24070Sstevel@tonic-gate 	}
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	if (flag)
24100Sstevel@tonic-gate 		*flag = trs_flag;
24110Sstevel@tonic-gate 	if (lwp)
24120Sstevel@tonic-gate 		*lwp = tid;
24130Sstevel@tonic-gate 	if (ss != NULL)
24140Sstevel@tonic-gate 		(void) _thrp_stksegment(ulwp, ss);
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 	ulwp_unlock(ulwp, udp);
24170Sstevel@tonic-gate 	return (error);
24180Sstevel@tonic-gate }
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate /*
24210Sstevel@tonic-gate  * Set the appropriate register state for the target thread.
24220Sstevel@tonic-gate  * This is not used by java.  It exists solely for the MSTC test suite.
24230Sstevel@tonic-gate  */
24246812Sraf #pragma weak _thr_setstate = thr_setstate
24250Sstevel@tonic-gate int
24266812Sraf thr_setstate(thread_t tid, int flag, gregset_t rs)
24270Sstevel@tonic-gate {
24280Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
24290Sstevel@tonic-gate 	ulwp_t *ulwp;
24300Sstevel@tonic-gate 	int error = 0;
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate 	if ((ulwp = find_lwp(tid)) == NULL)
24330Sstevel@tonic-gate 		return (ESRCH);
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate 	if (!ulwp->ul_stop && !suspendedallmutators)
24360Sstevel@tonic-gate 		error = EINVAL;
24370Sstevel@tonic-gate 	else if (rs != NULL) {
24380Sstevel@tonic-gate 		switch (flag) {
24390Sstevel@tonic-gate 		case TRS_NONVOLATILE:
24400Sstevel@tonic-gate 			/* do /proc stuff here? */
24410Sstevel@tonic-gate 			if (ulwp->ul_stop)
24420Sstevel@tonic-gate 				setgregs(ulwp, rs);
24430Sstevel@tonic-gate 			else
24440Sstevel@tonic-gate 				error = EINVAL;
24450Sstevel@tonic-gate 			break;
24460Sstevel@tonic-gate 		case TRS_LWPID:		/* do /proc stuff here? */
24470Sstevel@tonic-gate 		default:
24480Sstevel@tonic-gate 			error = EINVAL;
24490Sstevel@tonic-gate 			break;
24500Sstevel@tonic-gate 		}
24510Sstevel@tonic-gate 	}
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 	ulwp_unlock(ulwp, udp);
24540Sstevel@tonic-gate 	return (error);
24550Sstevel@tonic-gate }
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate int
24580Sstevel@tonic-gate getlwpstatus(thread_t tid, struct lwpstatus *sp)
24590Sstevel@tonic-gate {
24605891Sraf 	extern ssize_t __pread(int, void *, size_t, off_t);
24610Sstevel@tonic-gate 	char buf[100];
24620Sstevel@tonic-gate 	int fd;
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 	/* "/proc/self/lwp/%u/lwpstatus" w/o stdio */
24650Sstevel@tonic-gate 	(void) strcpy(buf, "/proc/self/lwp/");
24660Sstevel@tonic-gate 	ultos((uint64_t)tid, 10, buf + strlen(buf));
24670Sstevel@tonic-gate 	(void) strcat(buf, "/lwpstatus");
24686515Sraf 	if ((fd = __open(buf, O_RDONLY, 0)) >= 0) {
24695891Sraf 		while (__pread(fd, sp, sizeof (*sp), 0) == sizeof (*sp)) {
24700Sstevel@tonic-gate 			if (sp->pr_flags & PR_STOPPED) {
24716515Sraf 				(void) __close(fd);
24720Sstevel@tonic-gate 				return (0);
24730Sstevel@tonic-gate 			}
24746515Sraf 			yield();	/* give him a chance to stop */
24750Sstevel@tonic-gate 		}
24766515Sraf 		(void) __close(fd);
24770Sstevel@tonic-gate 	}
24780Sstevel@tonic-gate 	return (-1);
24790Sstevel@tonic-gate }
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate int
24820Sstevel@tonic-gate putlwpregs(thread_t tid, prgregset_t prp)
24830Sstevel@tonic-gate {
24845891Sraf 	extern ssize_t __writev(int, const struct iovec *, int);
24850Sstevel@tonic-gate 	char buf[100];
24860Sstevel@tonic-gate 	int fd;
24870Sstevel@tonic-gate 	long dstop_sreg[2];
24880Sstevel@tonic-gate 	long run_null[2];
24890Sstevel@tonic-gate 	iovec_t iov[3];
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate 	/* "/proc/self/lwp/%u/lwpctl" w/o stdio */
24920Sstevel@tonic-gate 	(void) strcpy(buf, "/proc/self/lwp/");
24930Sstevel@tonic-gate 	ultos((uint64_t)tid, 10, buf + strlen(buf));
24940Sstevel@tonic-gate 	(void) strcat(buf, "/lwpctl");
24956515Sraf 	if ((fd = __open(buf, O_WRONLY, 0)) >= 0) {
24960Sstevel@tonic-gate 		dstop_sreg[0] = PCDSTOP;	/* direct it to stop */
24970Sstevel@tonic-gate 		dstop_sreg[1] = PCSREG;		/* set the registers */
24980Sstevel@tonic-gate 		iov[0].iov_base = (caddr_t)dstop_sreg;
24990Sstevel@tonic-gate 		iov[0].iov_len = sizeof (dstop_sreg);
25000Sstevel@tonic-gate 		iov[1].iov_base = (caddr_t)prp;	/* from the register set */
25010Sstevel@tonic-gate 		iov[1].iov_len = sizeof (prgregset_t);
25020Sstevel@tonic-gate 		run_null[0] = PCRUN;		/* make it runnable again */
25030Sstevel@tonic-gate 		run_null[1] = 0;
25040Sstevel@tonic-gate 		iov[2].iov_base = (caddr_t)run_null;
25050Sstevel@tonic-gate 		iov[2].iov_len = sizeof (run_null);
25065891Sraf 		if (__writev(fd, iov, 3) >= 0) {
25076515Sraf 			(void) __close(fd);
25080Sstevel@tonic-gate 			return (0);
25090Sstevel@tonic-gate 		}
25106515Sraf 		(void) __close(fd);
25110Sstevel@tonic-gate 	}
25120Sstevel@tonic-gate 	return (-1);
25130Sstevel@tonic-gate }
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate static ulong_t
25160Sstevel@tonic-gate gettsp_slow(thread_t tid)
25170Sstevel@tonic-gate {
25180Sstevel@tonic-gate 	char buf[100];
25190Sstevel@tonic-gate 	struct lwpstatus status;
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	if (getlwpstatus(tid, &status) != 0) {
25220Sstevel@tonic-gate 		/* "__gettsp(%u): can't read lwpstatus" w/o stdio */
25230Sstevel@tonic-gate 		(void) strcpy(buf, "__gettsp(");
25240Sstevel@tonic-gate 		ultos((uint64_t)tid, 10, buf + strlen(buf));
25250Sstevel@tonic-gate 		(void) strcat(buf, "): can't read lwpstatus");
25260Sstevel@tonic-gate 		thr_panic(buf);
25270Sstevel@tonic-gate 	}
25280Sstevel@tonic-gate 	return (status.pr_reg[R_SP]);
25290Sstevel@tonic-gate }
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate ulong_t
25320Sstevel@tonic-gate __gettsp(thread_t tid)
25330Sstevel@tonic-gate {
25340Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
25350Sstevel@tonic-gate 	ulwp_t *ulwp;
25360Sstevel@tonic-gate 	ulong_t result;
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	if ((ulwp = find_lwp(tid)) == NULL)
25390Sstevel@tonic-gate 		return (0);
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 	if (ulwp->ul_stop && (result = ulwp->ul_sp) != 0) {
25420Sstevel@tonic-gate 		ulwp_unlock(ulwp, udp);
25430Sstevel@tonic-gate 		return (result);
25440Sstevel@tonic-gate 	}
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 	result = gettsp_slow(tid);
25470Sstevel@tonic-gate 	ulwp_unlock(ulwp, udp);
25480Sstevel@tonic-gate 	return (result);
25490Sstevel@tonic-gate }
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate /*
25520Sstevel@tonic-gate  * This tells java stack walkers how to find the ucontext
25530Sstevel@tonic-gate  * structure passed to signal handlers.
25540Sstevel@tonic-gate  */
25556812Sraf #pragma weak _thr_sighndlrinfo = thr_sighndlrinfo
25560Sstevel@tonic-gate void
25576812Sraf thr_sighndlrinfo(void (**func)(), int *funcsize)
25580Sstevel@tonic-gate {
25590Sstevel@tonic-gate 	*func = &__sighndlr;
25600Sstevel@tonic-gate 	*funcsize = (char *)&__sighndlrend - (char *)&__sighndlr;
25610Sstevel@tonic-gate }
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate /*
25640Sstevel@tonic-gate  * Mark a thread a mutator or reset a mutator to being a default,
25650Sstevel@tonic-gate  * non-mutator thread.
25660Sstevel@tonic-gate  */
25676812Sraf #pragma weak _thr_setmutator = thr_setmutator
25680Sstevel@tonic-gate int
25696812Sraf thr_setmutator(thread_t tid, int enabled)
25700Sstevel@tonic-gate {
25710Sstevel@tonic-gate 	ulwp_t *self = curthread;
25720Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
25730Sstevel@tonic-gate 	ulwp_t *ulwp;
25740Sstevel@tonic-gate 	int error;
25755891Sraf 	int cancel_state;
25760Sstevel@tonic-gate 
25775891Sraf 	enabled = enabled? 1 : 0;
25780Sstevel@tonic-gate top:
25790Sstevel@tonic-gate 	if (tid == 0) {
25800Sstevel@tonic-gate 		ulwp = self;
25810Sstevel@tonic-gate 		ulwp_lock(ulwp, udp);
25820Sstevel@tonic-gate 	} else if ((ulwp = find_lwp(tid)) == NULL) {
25830Sstevel@tonic-gate 		return (ESRCH);
25840Sstevel@tonic-gate 	}
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate 	/*
25870Sstevel@tonic-gate 	 * The target thread should be the caller itself or a suspended thread.
25880Sstevel@tonic-gate 	 * This prevents the target from also changing its ul_mutator field.
25890Sstevel@tonic-gate 	 */
25900Sstevel@tonic-gate 	error = 0;
25910Sstevel@tonic-gate 	if (ulwp != self && !ulwp->ul_stop && enabled)
25920Sstevel@tonic-gate 		error = EINVAL;
25930Sstevel@tonic-gate 	else if (ulwp->ul_mutator != enabled) {
25940Sstevel@tonic-gate 		lmutex_lock(&mutatorslock);
25950Sstevel@tonic-gate 		if (mutatorsbarrier) {
25960Sstevel@tonic-gate 			ulwp_unlock(ulwp, udp);
25976812Sraf 			(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
25985891Sraf 			    &cancel_state);
25990Sstevel@tonic-gate 			while (mutatorsbarrier)
26006812Sraf 				(void) cond_wait(&mutatorscv, &mutatorslock);
26016812Sraf 			(void) pthread_setcancelstate(cancel_state, NULL);
26020Sstevel@tonic-gate 			lmutex_unlock(&mutatorslock);
26030Sstevel@tonic-gate 			goto top;
26040Sstevel@tonic-gate 		}
26050Sstevel@tonic-gate 		ulwp->ul_mutator = enabled;
26060Sstevel@tonic-gate 		lmutex_unlock(&mutatorslock);
26070Sstevel@tonic-gate 	}
26080Sstevel@tonic-gate 
26090Sstevel@tonic-gate 	ulwp_unlock(ulwp, udp);
26100Sstevel@tonic-gate 	return (error);
26110Sstevel@tonic-gate }
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate /*
26140Sstevel@tonic-gate  * Establish a barrier against new mutators.  Any non-mutator trying
26150Sstevel@tonic-gate  * to become a mutator is suspended until the barrier is removed.
26160Sstevel@tonic-gate  */
26176812Sraf #pragma weak _thr_mutators_barrier = thr_mutators_barrier
26180Sstevel@tonic-gate void
26196812Sraf thr_mutators_barrier(int enabled)
26200Sstevel@tonic-gate {
26210Sstevel@tonic-gate 	int oldvalue;
26225891Sraf 	int cancel_state;
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	lmutex_lock(&mutatorslock);
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	/*
26270Sstevel@tonic-gate 	 * Wait if trying to set the barrier while it is already set.
26280Sstevel@tonic-gate 	 */
26296812Sraf 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
26300Sstevel@tonic-gate 	while (mutatorsbarrier && enabled)
26316812Sraf 		(void) cond_wait(&mutatorscv, &mutatorslock);
26326812Sraf 	(void) pthread_setcancelstate(cancel_state, NULL);
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 	oldvalue = mutatorsbarrier;
26350Sstevel@tonic-gate 	mutatorsbarrier = enabled;
26360Sstevel@tonic-gate 	/*
26370Sstevel@tonic-gate 	 * Wakeup any blocked non-mutators when barrier is removed.
26380Sstevel@tonic-gate 	 */
26390Sstevel@tonic-gate 	if (oldvalue && !enabled)
26406812Sraf 		(void) cond_broadcast(&mutatorscv);
26410Sstevel@tonic-gate 	lmutex_unlock(&mutatorslock);
26420Sstevel@tonic-gate }
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate /*
26450Sstevel@tonic-gate  * Suspend the set of all mutators except for the caller.  The list
26460Sstevel@tonic-gate  * of actively running threads is searched and only the mutators
26470Sstevel@tonic-gate  * in this list are suspended.  Actively running non-mutators remain
26480Sstevel@tonic-gate  * running.  Any other thread is suspended.
26490Sstevel@tonic-gate  */
26506812Sraf #pragma weak _thr_suspend_allmutators = thr_suspend_allmutators
26510Sstevel@tonic-gate int
26526812Sraf thr_suspend_allmutators(void)
26530Sstevel@tonic-gate {
26540Sstevel@tonic-gate 	ulwp_t *self = curthread;
26550Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
26560Sstevel@tonic-gate 	ulwp_t *ulwp;
26570Sstevel@tonic-gate 	int link_dropped;
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 	/*
26603512Sraf 	 * We single-thread the entire thread suspend/continue mechanism.
26610Sstevel@tonic-gate 	 */
26624843Sraf 	fork_lock_enter();
26633512Sraf 
26640Sstevel@tonic-gate top:
26650Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	if (suspendingallmutators || suspendedallmutators) {
26680Sstevel@tonic-gate 		lmutex_unlock(&udp->link_lock);
26690Sstevel@tonic-gate 		fork_lock_exit();
26700Sstevel@tonic-gate 		return (EINVAL);
26710Sstevel@tonic-gate 	}
26720Sstevel@tonic-gate 	suspendingallmutators = 1;
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
26750Sstevel@tonic-gate 		ulwp_lock(ulwp, udp);
26760Sstevel@tonic-gate 		if (!ulwp->ul_mutator) {
26770Sstevel@tonic-gate 			ulwp_unlock(ulwp, udp);
26780Sstevel@tonic-gate 		} else if (ulwp->ul_stop) {	/* already stopped */
26790Sstevel@tonic-gate 			ulwp->ul_stop |= TSTP_MUTATOR;
26800Sstevel@tonic-gate 			ulwp_broadcast(ulwp);
26810Sstevel@tonic-gate 			ulwp_unlock(ulwp, udp);
26820Sstevel@tonic-gate 		} else {
26830Sstevel@tonic-gate 			/*
26840Sstevel@tonic-gate 			 * Move the stopped lwp out of a critical section.
26850Sstevel@tonic-gate 			 */
26860Sstevel@tonic-gate 			if (safe_suspend(ulwp, TSTP_MUTATOR, &link_dropped) ||
26870Sstevel@tonic-gate 			    link_dropped) {
26880Sstevel@tonic-gate 				suspendingallmutators = 0;
26890Sstevel@tonic-gate 				goto top;
26900Sstevel@tonic-gate 			}
26910Sstevel@tonic-gate 		}
26920Sstevel@tonic-gate 	}
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 	suspendedallmutators = 1;
26950Sstevel@tonic-gate 	suspendingallmutators = 0;
26960Sstevel@tonic-gate 	lmutex_unlock(&udp->link_lock);
26970Sstevel@tonic-gate 	fork_lock_exit();
26980Sstevel@tonic-gate 	return (0);
26990Sstevel@tonic-gate }
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate /*
27020Sstevel@tonic-gate  * Suspend the target mutator.  The caller is permitted to suspend
27030Sstevel@tonic-gate  * itself.  If a mutator barrier is enabled, the caller will suspend
27040Sstevel@tonic-gate  * itself as though it had been suspended by thr_suspend_allmutators().
27050Sstevel@tonic-gate  * When the barrier is removed, this thread will be resumed.  Any
27060Sstevel@tonic-gate  * suspended mutator, whether suspended by thr_suspend_mutator(), or by
27070Sstevel@tonic-gate  * thr_suspend_allmutators(), can be resumed by thr_continue_mutator().
27080Sstevel@tonic-gate  */
27096812Sraf #pragma weak _thr_suspend_mutator = thr_suspend_mutator
27100Sstevel@tonic-gate int
27116812Sraf thr_suspend_mutator(thread_t tid)
27120Sstevel@tonic-gate {
27130Sstevel@tonic-gate 	if (tid == 0)
27140Sstevel@tonic-gate 		tid = curthread->ul_lwpid;
27150Sstevel@tonic-gate 	return (_thrp_suspend(tid, TSTP_MUTATOR));
27160Sstevel@tonic-gate }
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate /*
27190Sstevel@tonic-gate  * Resume the set of all suspended mutators.
27200Sstevel@tonic-gate  */
27216812Sraf #pragma weak _thr_continue_allmutators = thr_continue_allmutators
27220Sstevel@tonic-gate int
27236812Sraf thr_continue_allmutators()
27240Sstevel@tonic-gate {
27250Sstevel@tonic-gate 	ulwp_t *self = curthread;
27260Sstevel@tonic-gate 	uberdata_t *udp = self->ul_uberdata;
27270Sstevel@tonic-gate 	ulwp_t *ulwp;
27283512Sraf 
27293512Sraf 	/*
27303512Sraf 	 * We single-thread the entire thread suspend/continue mechanism.
27313512Sraf 	 */
27324843Sraf 	fork_lock_enter();
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 	lmutex_lock(&udp->link_lock);
27350Sstevel@tonic-gate 	if (!suspendedallmutators) {
27360Sstevel@tonic-gate 		lmutex_unlock(&udp->link_lock);
27373512Sraf 		fork_lock_exit();
27380Sstevel@tonic-gate 		return (EINVAL);
27390Sstevel@tonic-gate 	}
27400Sstevel@tonic-gate 	suspendedallmutators = 0;
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 	for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
27430Sstevel@tonic-gate 		mutex_t *mp = ulwp_mutex(ulwp, udp);
27440Sstevel@tonic-gate 		lmutex_lock(mp);
27450Sstevel@tonic-gate 		if (ulwp->ul_stop & TSTP_MUTATOR) {
27460Sstevel@tonic-gate 			ulwp->ul_stop &= ~TSTP_MUTATOR;
27470Sstevel@tonic-gate 			ulwp_broadcast(ulwp);
27480Sstevel@tonic-gate 			if (!ulwp->ul_stop)
27490Sstevel@tonic-gate 				force_continue(ulwp);
27500Sstevel@tonic-gate 		}
27510Sstevel@tonic-gate 		lmutex_unlock(mp);
27520Sstevel@tonic-gate 	}
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 	lmutex_unlock(&udp->link_lock);
27553512Sraf 	fork_lock_exit();
27560Sstevel@tonic-gate 	return (0);
27570Sstevel@tonic-gate }
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate /*
27600Sstevel@tonic-gate  * Resume a suspended mutator.
27610Sstevel@tonic-gate  */
27626812Sraf #pragma weak _thr_continue_mutator = thr_continue_mutator
27630Sstevel@tonic-gate int
27646812Sraf thr_continue_mutator(thread_t tid)
27650Sstevel@tonic-gate {
27660Sstevel@tonic-gate 	return (_thrp_continue(tid, TSTP_MUTATOR));
27670Sstevel@tonic-gate }
27680Sstevel@tonic-gate 
27696812Sraf #pragma weak _thr_wait_mutator = thr_wait_mutator
27700Sstevel@tonic-gate int
27716812Sraf thr_wait_mutator(thread_t tid, int dontwait)
27720Sstevel@tonic-gate {
27730Sstevel@tonic-gate 	uberdata_t *udp = curthread->ul_uberdata;
27740Sstevel@tonic-gate 	ulwp_t *ulwp;
27755891Sraf 	int cancel_state;
27760Sstevel@tonic-gate 	int error = 0;
27770Sstevel@tonic-gate 
27786812Sraf 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
27790Sstevel@tonic-gate top:
27805891Sraf 	if ((ulwp = find_lwp(tid)) == NULL) {
27816812Sraf 		(void) pthread_setcancelstate(cancel_state, NULL);
27820Sstevel@tonic-gate 		return (ESRCH);
27835891Sraf 	}
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	if (!ulwp->ul_mutator)
27860Sstevel@tonic-gate 		error = EINVAL;
27870Sstevel@tonic-gate 	else if (dontwait) {
27880Sstevel@tonic-gate 		if (!(ulwp->ul_stop & TSTP_MUTATOR))
27890Sstevel@tonic-gate 			error = EWOULDBLOCK;
27900Sstevel@tonic-gate 	} else if (!(ulwp->ul_stop & TSTP_MUTATOR)) {
27910Sstevel@tonic-gate 		cond_t *cvp = ulwp_condvar(ulwp, udp);
27920Sstevel@tonic-gate 		mutex_t *mp = ulwp_mutex(ulwp, udp);
27930Sstevel@tonic-gate 
27946812Sraf 		(void) cond_wait(cvp, mp);
27950Sstevel@tonic-gate 		(void) lmutex_unlock(mp);
27960Sstevel@tonic-gate 		goto top;
27970Sstevel@tonic-gate 	}
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate 	ulwp_unlock(ulwp, udp);
28006812Sraf 	(void) pthread_setcancelstate(cancel_state, NULL);
28010Sstevel@tonic-gate 	return (error);
28020Sstevel@tonic-gate }
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate /* PROBE_SUPPORT begin */
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate void
28070Sstevel@tonic-gate thr_probe_setup(void *data)
28080Sstevel@tonic-gate {
28090Sstevel@tonic-gate 	curthread->ul_tpdp = data;
28100Sstevel@tonic-gate }
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate static void *
28130Sstevel@tonic-gate _thread_probe_getfunc()
28140Sstevel@tonic-gate {
28150Sstevel@tonic-gate 	return (curthread->ul_tpdp);
28160Sstevel@tonic-gate }
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate void * (*thr_probe_getfunc_addr)(void) = _thread_probe_getfunc;
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate /* ARGSUSED */
28210Sstevel@tonic-gate void
28220Sstevel@tonic-gate _resume(ulwp_t *ulwp, caddr_t sp, int dontsave)
28230Sstevel@tonic-gate {
28240Sstevel@tonic-gate 	/* never called */
28250Sstevel@tonic-gate }
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate /* ARGSUSED */
28280Sstevel@tonic-gate void
28290Sstevel@tonic-gate _resume_ret(ulwp_t *oldlwp)
28300Sstevel@tonic-gate {
28310Sstevel@tonic-gate 	/* never called */
28320Sstevel@tonic-gate }
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate /* PROBE_SUPPORT end */
2835