xref: /titanic_52/usr/src/lib/libc/port/threads/thr.c (revision c7158ae983f5a04c4a998f468ecefba6d23ba721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "lint.h"
30 #include "thr_uberdata.h"
31 #include <pthread.h>
32 #include <procfs.h>
33 #include <sys/uio.h>
34 #include <ctype.h>
35 #include "libc.h"
36 
37 #undef errno
38 extern int errno;
39 
40 /*
41  * Between Solaris 2.5 and Solaris 9, __threaded was used to indicate
42  * "we are linked with libthread".  The Sun Workshop 6 update 1 compilation
43  * system used it illegally (it is a consolidation private symbol).
44  * To accommodate this and possibly other abusers of the symbol,
45  * we make it always equal to 1 now that libthread has been folded
46  * into libc.  The new __libc_threaded symbol is used to indicate
47  * the new meaning, "more than one thread exists".
48  */
49 int __threaded = 1;		/* always equal to 1 */
50 int __libc_threaded = 0;	/* zero until first thr_create() */
51 
52 /*
53  * thr_concurrency and pthread_concurrency are not used by the library.
54  * They exist solely to hold and return the values set by calls to
55  * thr_setconcurrency() and pthread_setconcurrency().
56  * Because thr_concurrency is affected by the THR_NEW_LWP flag
57  * to thr_create(), thr_concurrency is protected by link_lock.
58  */
59 static	int	thr_concurrency = 1;
60 static	int	pthread_concurrency;
61 
62 #define	HASHTBLSZ	1024	/* must be a power of two */
63 #define	TIDHASH(tid, udp)	(tid & (udp)->hash_mask)
64 
65 /* initial allocation, just enough for one lwp */
66 #pragma align 64(init_hash_table)
67 thr_hash_table_t init_hash_table[1] = {
68 	{ DEFAULTMUTEX, DEFAULTCV, NULL },
69 };
70 
71 extern const Lc_interface rtld_funcs[];
72 
73 /*
74  * The weak version is known to libc_db and mdb.
75  */
76 #pragma weak _uberdata = __uberdata
77 uberdata_t __uberdata = {
78 	{ DEFAULTMUTEX, NULL, 0 },	/* link_lock */
79 	{ RECURSIVEMUTEX, NULL, 0 },	/* ld_lock */
80 	{ RECURSIVEMUTEX, NULL, 0 },	/* fork_lock */
81 	{ RECURSIVEMUTEX, NULL, 0 },	/* atfork_lock */
82 	{ RECURSIVEMUTEX, NULL, 0 },	/* callout_lock */
83 	{ DEFAULTMUTEX, NULL, 0 },	/* tdb_hash_lock */
84 	{ 0, },				/* tdb_hash_lock_stats */
85 	{ { 0 }, },			/* siguaction[NSIG] */
86 	{{ DEFAULTMUTEX, NULL, 0 },		/* bucket[NBUCKETS] */
87 	{ DEFAULTMUTEX, NULL, 0 },
88 	{ DEFAULTMUTEX, NULL, 0 },
89 	{ DEFAULTMUTEX, NULL, 0 },
90 	{ DEFAULTMUTEX, NULL, 0 },
91 	{ DEFAULTMUTEX, NULL, 0 },
92 	{ DEFAULTMUTEX, NULL, 0 },
93 	{ DEFAULTMUTEX, NULL, 0 },
94 	{ DEFAULTMUTEX, NULL, 0 },
95 	{ DEFAULTMUTEX, NULL, 0 }},
96 	{ RECURSIVEMUTEX, NULL, NULL },		/* atexit_root */
97 	{ DEFAULTMUTEX, 0, 0, NULL },		/* tsd_metadata */
98 	{ DEFAULTMUTEX, {0, 0}, {0, 0} },	/* tls_metadata */
99 	0,			/* primary_map */
100 	0,			/* bucket_init */
101 	0,			/* pad[0] */
102 	0,			/* pad[1] */
103 	{ 0 },			/* uberflags */
104 	NULL,			/* queue_head */
105 	init_hash_table,	/* thr_hash_table */
106 	1,			/* hash_size: size of the hash table */
107 	0,			/* hash_mask: hash_size - 1 */
108 	NULL,			/* ulwp_one */
109 	NULL,			/* all_lwps */
110 	NULL,			/* all_zombies */
111 	0,			/* nthreads */
112 	0,			/* nzombies */
113 	0,			/* ndaemons */
114 	0,			/* pid */
115 	sigacthandler,		/* sigacthandler */
116 	NULL,			/* lwp_stacks */
117 	NULL,			/* lwp_laststack */
118 	0,			/* nfreestack */
119 	10,			/* thread_stack_cache */
120 	NULL,			/* ulwp_freelist */
121 	NULL,			/* ulwp_lastfree */
122 	NULL,			/* ulwp_replace_free */
123 	NULL,			/* ulwp_replace_last */
124 	NULL,			/* atforklist */
125 	NULL,			/* robustlocks */
126 	NULL,			/* __tdb_bootstrap */
127 	{			/* tdb */
128 		NULL,		/* tdb_sync_addr_hash */
129 		0,		/* tdb_register_count */
130 		0,		/* tdb_hash_alloc_failed */
131 		NULL,		/* tdb_sync_addr_free */
132 		NULL,		/* tdb_sync_addr_last */
133 		0,		/* tdb_sync_alloc */
134 		{ 0, 0 },	/* tdb_ev_global_mask */
135 		tdb_events,	/* tdb_events array */
136 	},
137 };
138 
139 /*
140  * The weak version is known to libc_db and mdb.
141  */
142 #pragma weak _tdb_bootstrap = __tdb_bootstrap
143 uberdata_t **__tdb_bootstrap = NULL;
144 
145 int	thread_queue_fifo = 4;
146 int	thread_queue_dump = 0;
147 int	thread_cond_wait_defer = 0;
148 int	thread_error_detection = 0;
149 int	thread_async_safe = 0;
150 int	thread_stack_cache = 10;
151 
152 int	thread_door_noreserve = 0;
153 
154 static	ulwp_t	*ulwp_alloc(void);
155 static	void	ulwp_free(ulwp_t *);
156 
157 /*
158  * Insert the lwp into the hash table.
159  */
160 void
161 hash_in_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
162 {
163 	ulwp->ul_hash = udp->thr_hash_table[ix].hash_bucket;
164 	udp->thr_hash_table[ix].hash_bucket = ulwp;
165 	ulwp->ul_ix = ix;
166 }
167 
168 void
169 hash_in(ulwp_t *ulwp, uberdata_t *udp)
170 {
171 	int ix = TIDHASH(ulwp->ul_lwpid, udp);
172 	mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
173 
174 	lmutex_lock(mp);
175 	hash_in_unlocked(ulwp, ix, udp);
176 	lmutex_unlock(mp);
177 }
178 
179 /*
180  * Delete the lwp from the hash table.
181  */
182 void
183 hash_out_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp)
184 {
185 	ulwp_t **ulwpp;
186 
187 	for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
188 	    ulwp != *ulwpp;
189 	    ulwpp = &(*ulwpp)->ul_hash)
190 		;
191 	*ulwpp = ulwp->ul_hash;
192 	ulwp->ul_hash = NULL;
193 	ulwp->ul_ix = -1;
194 }
195 
196 void
197 hash_out(ulwp_t *ulwp, uberdata_t *udp)
198 {
199 	int ix;
200 
201 	if ((ix = ulwp->ul_ix) >= 0) {
202 		mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
203 
204 		lmutex_lock(mp);
205 		hash_out_unlocked(ulwp, ix, udp);
206 		lmutex_unlock(mp);
207 	}
208 }
209 
210 /*
211  * Retain stack information for thread structures that are being recycled for
212  * new threads.  All other members of the thread structure should be zeroed.
213  */
214 static void
215 ulwp_clean(ulwp_t *ulwp)
216 {
217 	caddr_t stk = ulwp->ul_stk;
218 	size_t mapsiz = ulwp->ul_mapsiz;
219 	size_t guardsize = ulwp->ul_guardsize;
220 	uintptr_t stktop = ulwp->ul_stktop;
221 	size_t stksiz = ulwp->ul_stksiz;
222 
223 	(void) memset(ulwp, 0, sizeof (*ulwp));
224 
225 	ulwp->ul_stk = stk;
226 	ulwp->ul_mapsiz = mapsiz;
227 	ulwp->ul_guardsize = guardsize;
228 	ulwp->ul_stktop = stktop;
229 	ulwp->ul_stksiz = stksiz;
230 }
231 
232 static int stackprot;
233 
234 /*
235  * Answer the question, "Is the lwp in question really dead?"
236  * We must inquire of the operating system to be really sure
237  * because the lwp may have called lwp_exit() but it has not
238  * yet completed the exit.
239  */
240 static int
241 dead_and_buried(ulwp_t *ulwp)
242 {
243 	if (ulwp->ul_lwpid == (lwpid_t)(-1))
244 		return (1);
245 	if (ulwp->ul_dead && ulwp->ul_detached &&
246 	    __lwp_kill(ulwp->ul_lwpid, 0) == ESRCH) {
247 		ulwp->ul_lwpid = (lwpid_t)(-1);
248 		return (1);
249 	}
250 	return (0);
251 }
252 
253 /*
254  * Attempt to keep the stack cache within the specified cache limit.
255  */
256 static void
257 trim_stack_cache(int cache_limit)
258 {
259 	ulwp_t *self = curthread;
260 	uberdata_t *udp = self->ul_uberdata;
261 	ulwp_t *prev = NULL;
262 	ulwp_t **ulwpp = &udp->lwp_stacks;
263 	ulwp_t *ulwp;
264 
265 	ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, self));
266 
267 	while (udp->nfreestack > cache_limit && (ulwp = *ulwpp) != NULL) {
268 		if (dead_and_buried(ulwp)) {
269 			*ulwpp = ulwp->ul_next;
270 			if (ulwp == udp->lwp_laststack)
271 				udp->lwp_laststack = prev;
272 			hash_out(ulwp, udp);
273 			udp->nfreestack--;
274 			(void) munmap(ulwp->ul_stk, ulwp->ul_mapsiz);
275 			/*
276 			 * Now put the free ulwp on the ulwp freelist.
277 			 */
278 			ulwp->ul_mapsiz = 0;
279 			ulwp->ul_next = NULL;
280 			if (udp->ulwp_freelist == NULL)
281 				udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
282 			else {
283 				udp->ulwp_lastfree->ul_next = ulwp;
284 				udp->ulwp_lastfree = ulwp;
285 			}
286 		} else {
287 			prev = ulwp;
288 			ulwpp = &ulwp->ul_next;
289 		}
290 	}
291 }
292 
293 /*
294  * Find an unused stack of the requested size
295  * or create a new stack of the requested size.
296  * Return a pointer to the ulwp_t structure referring to the stack, or NULL.
297  * thr_exit() stores 1 in the ul_dead member.
298  * thr_join() stores -1 in the ul_lwpid member.
299  */
300 ulwp_t *
301 find_stack(size_t stksize, size_t guardsize)
302 {
303 	static size_t pagesize = 0;
304 
305 	uberdata_t *udp = curthread->ul_uberdata;
306 	size_t mapsize;
307 	ulwp_t *prev;
308 	ulwp_t *ulwp;
309 	ulwp_t **ulwpp;
310 	void *stk;
311 
312 	/*
313 	 * The stack is allocated PROT_READ|PROT_WRITE|PROT_EXEC
314 	 * unless overridden by the system's configuration.
315 	 */
316 	if (stackprot == 0) {	/* do this once */
317 		long lprot = _sysconf(_SC_STACK_PROT);
318 		if (lprot <= 0)
319 			lprot = (PROT_READ|PROT_WRITE|PROT_EXEC);
320 		stackprot = (int)lprot;
321 	}
322 	if (pagesize == 0)	/* do this once */
323 		pagesize = _sysconf(_SC_PAGESIZE);
324 
325 	/*
326 	 * One megabyte stacks by default, but subtract off
327 	 * two pages for the system-created red zones.
328 	 * Round up a non-zero stack size to a pagesize multiple.
329 	 */
330 	if (stksize == 0)
331 		stksize = DEFAULTSTACK - 2 * pagesize;
332 	else
333 		stksize = ((stksize + pagesize - 1) & -pagesize);
334 
335 	/*
336 	 * Round up the mapping size to a multiple of pagesize.
337 	 * Note: mmap() provides at least one page of red zone
338 	 * so we deduct that from the value of guardsize.
339 	 */
340 	if (guardsize != 0)
341 		guardsize = ((guardsize + pagesize - 1) & -pagesize) - pagesize;
342 	mapsize = stksize + guardsize;
343 
344 	lmutex_lock(&udp->link_lock);
345 	for (prev = NULL, ulwpp = &udp->lwp_stacks;
346 	    (ulwp = *ulwpp) != NULL;
347 	    prev = ulwp, ulwpp = &ulwp->ul_next) {
348 		if (ulwp->ul_mapsiz == mapsize &&
349 		    ulwp->ul_guardsize == guardsize &&
350 		    dead_and_buried(ulwp)) {
351 			/*
352 			 * The previous lwp is gone; reuse the stack.
353 			 * Remove the ulwp from the stack list.
354 			 */
355 			*ulwpp = ulwp->ul_next;
356 			ulwp->ul_next = NULL;
357 			if (ulwp == udp->lwp_laststack)
358 				udp->lwp_laststack = prev;
359 			hash_out(ulwp, udp);
360 			udp->nfreestack--;
361 			lmutex_unlock(&udp->link_lock);
362 			ulwp_clean(ulwp);
363 			return (ulwp);
364 		}
365 	}
366 
367 	/*
368 	 * None of the cached stacks matched our mapping size.
369 	 * Reduce the stack cache to get rid of possibly
370 	 * very old stacks that will never be reused.
371 	 */
372 	if (udp->nfreestack > udp->thread_stack_cache)
373 		trim_stack_cache(udp->thread_stack_cache);
374 	else if (udp->nfreestack > 0)
375 		trim_stack_cache(udp->nfreestack - 1);
376 	lmutex_unlock(&udp->link_lock);
377 
378 	/*
379 	 * Create a new stack.
380 	 */
381 	if ((stk = mmap(NULL, mapsize, stackprot,
382 	    MAP_PRIVATE|MAP_NORESERVE|MAP_ANON, -1, (off_t)0)) != MAP_FAILED) {
383 		/*
384 		 * We have allocated our stack.  Now allocate the ulwp.
385 		 */
386 		ulwp = ulwp_alloc();
387 		if (ulwp == NULL)
388 			(void) munmap(stk, mapsize);
389 		else {
390 			ulwp->ul_stk = stk;
391 			ulwp->ul_mapsiz = mapsize;
392 			ulwp->ul_guardsize = guardsize;
393 			ulwp->ul_stktop = (uintptr_t)stk + mapsize;
394 			ulwp->ul_stksiz = stksize;
395 			ulwp->ul_ix = -1;
396 			if (guardsize)	/* protect the extra red zone */
397 				(void) mprotect(stk, guardsize, PROT_NONE);
398 		}
399 	}
400 	return (ulwp);
401 }
402 
403 /*
404  * Get a ulwp_t structure from the free list or allocate a new one.
405  * Such ulwp_t's do not have a stack allocated by the library.
406  */
407 static ulwp_t *
408 ulwp_alloc(void)
409 {
410 	ulwp_t *self = curthread;
411 	uberdata_t *udp = self->ul_uberdata;
412 	size_t tls_size;
413 	ulwp_t *prev;
414 	ulwp_t *ulwp;
415 	ulwp_t **ulwpp;
416 	caddr_t data;
417 
418 	lmutex_lock(&udp->link_lock);
419 	for (prev = NULL, ulwpp = &udp->ulwp_freelist;
420 	    (ulwp = *ulwpp) != NULL;
421 	    prev = ulwp, ulwpp = &ulwp->ul_next) {
422 		if (dead_and_buried(ulwp)) {
423 			*ulwpp = ulwp->ul_next;
424 			ulwp->ul_next = NULL;
425 			if (ulwp == udp->ulwp_lastfree)
426 				udp->ulwp_lastfree = prev;
427 			hash_out(ulwp, udp);
428 			lmutex_unlock(&udp->link_lock);
429 			ulwp_clean(ulwp);
430 			return (ulwp);
431 		}
432 	}
433 	lmutex_unlock(&udp->link_lock);
434 
435 	tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
436 	data = lmalloc(sizeof (*ulwp) + tls_size);
437 	if (data != NULL) {
438 		/* LINTED pointer cast may result in improper alignment */
439 		ulwp = (ulwp_t *)(data + tls_size);
440 	}
441 	return (ulwp);
442 }
443 
444 /*
445  * Free a ulwp structure.
446  * If there is an associated stack, put it on the stack list and
447  * munmap() previously freed stacks up to the residual cache limit.
448  * Else put it on the ulwp free list and never call lfree() on it.
449  */
450 static void
451 ulwp_free(ulwp_t *ulwp)
452 {
453 	uberdata_t *udp = curthread->ul_uberdata;
454 
455 	ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, curthread));
456 	ulwp->ul_next = NULL;
457 	if (ulwp == udp->ulwp_one)	/* don't reuse the primoridal stack */
458 		/*EMPTY*/;
459 	else if (ulwp->ul_mapsiz != 0) {
460 		if (udp->lwp_stacks == NULL)
461 			udp->lwp_stacks = udp->lwp_laststack = ulwp;
462 		else {
463 			udp->lwp_laststack->ul_next = ulwp;
464 			udp->lwp_laststack = ulwp;
465 		}
466 		if (++udp->nfreestack > udp->thread_stack_cache)
467 			trim_stack_cache(udp->thread_stack_cache);
468 	} else {
469 		if (udp->ulwp_freelist == NULL)
470 			udp->ulwp_freelist = udp->ulwp_lastfree = ulwp;
471 		else {
472 			udp->ulwp_lastfree->ul_next = ulwp;
473 			udp->ulwp_lastfree = ulwp;
474 		}
475 	}
476 }
477 
478 /*
479  * Find a named lwp and return a pointer to its hash list location.
480  * On success, returns with the hash lock held.
481  */
482 ulwp_t **
483 find_lwpp(thread_t tid)
484 {
485 	uberdata_t *udp = curthread->ul_uberdata;
486 	int ix = TIDHASH(tid, udp);
487 	mutex_t *mp = &udp->thr_hash_table[ix].hash_lock;
488 	ulwp_t *ulwp;
489 	ulwp_t **ulwpp;
490 
491 	if (tid == 0)
492 		return (NULL);
493 
494 	lmutex_lock(mp);
495 	for (ulwpp = &udp->thr_hash_table[ix].hash_bucket;
496 	    (ulwp = *ulwpp) != NULL;
497 	    ulwpp = &ulwp->ul_hash) {
498 		if (ulwp->ul_lwpid == tid)
499 			return (ulwpp);
500 	}
501 	lmutex_unlock(mp);
502 	return (NULL);
503 }
504 
505 /*
506  * Wake up all lwps waiting on this lwp for some reason.
507  */
508 void
509 ulwp_broadcast(ulwp_t *ulwp)
510 {
511 	ulwp_t *self = curthread;
512 	uberdata_t *udp = self->ul_uberdata;
513 
514 	ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
515 	(void) cond_broadcast_internal(ulwp_condvar(ulwp, udp));
516 }
517 
518 /*
519  * Find a named lwp and return a pointer to it.
520  * Returns with the hash lock held.
521  */
522 ulwp_t *
523 find_lwp(thread_t tid)
524 {
525 	ulwp_t *self = curthread;
526 	uberdata_t *udp = self->ul_uberdata;
527 	ulwp_t *ulwp = NULL;
528 	ulwp_t **ulwpp;
529 
530 	if (self->ul_lwpid == tid) {
531 		ulwp = self;
532 		ulwp_lock(ulwp, udp);
533 	} else if ((ulwpp = find_lwpp(tid)) != NULL) {
534 		ulwp = *ulwpp;
535 	}
536 
537 	if (ulwp && ulwp->ul_dead) {
538 		ulwp_unlock(ulwp, udp);
539 		ulwp = NULL;
540 	}
541 
542 	return (ulwp);
543 }
544 
545 int
546 _thrp_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
547 	long flags, thread_t *new_thread, size_t guardsize)
548 {
549 	ulwp_t *self = curthread;
550 	uberdata_t *udp = self->ul_uberdata;
551 	ucontext_t uc;
552 	uint_t lwp_flags;
553 	thread_t tid;
554 	int error = 0;
555 	ulwp_t *ulwp;
556 
557 	/*
558 	 * Enforce the restriction of not creating any threads
559 	 * until the primary link map has been initialized.
560 	 * Also, disallow thread creation to a child of vfork().
561 	 */
562 	if (!self->ul_primarymap || self->ul_vfork)
563 		return (ENOTSUP);
564 
565 	if (udp->hash_size == 1)
566 		finish_init();
567 
568 	if ((stk || stksize) && stksize < MINSTACK)
569 		return (EINVAL);
570 
571 	if (stk == NULL) {
572 		if ((ulwp = find_stack(stksize, guardsize)) == NULL)
573 			return (ENOMEM);
574 		stksize = ulwp->ul_mapsiz - ulwp->ul_guardsize;
575 	} else {
576 		/* initialize the private stack */
577 		if ((ulwp = ulwp_alloc()) == NULL)
578 			return (ENOMEM);
579 		ulwp->ul_stk = stk;
580 		ulwp->ul_stktop = (uintptr_t)stk + stksize;
581 		ulwp->ul_stksiz = stksize;
582 		ulwp->ul_ix = -1;
583 	}
584 	ulwp->ul_errnop = &ulwp->ul_errno;
585 
586 	lwp_flags = LWP_SUSPENDED;
587 	if (flags & (THR_DETACHED|THR_DAEMON)) {
588 		flags |= THR_DETACHED;
589 		lwp_flags |= LWP_DETACHED;
590 	}
591 	if (flags & THR_DAEMON)
592 		lwp_flags |= LWP_DAEMON;
593 
594 	/* creating a thread: enforce mt-correctness in _mutex_lock() */
595 	self->ul_async_safe = 1;
596 
597 	/* per-thread copies of global variables, for speed */
598 	ulwp->ul_queue_fifo = self->ul_queue_fifo;
599 	ulwp->ul_cond_wait_defer = self->ul_cond_wait_defer;
600 	ulwp->ul_error_detection = self->ul_error_detection;
601 	ulwp->ul_async_safe = self->ul_async_safe;
602 	ulwp->ul_max_spinners = self->ul_max_spinners;
603 	ulwp->ul_adaptive_spin = self->ul_adaptive_spin;
604 	ulwp->ul_queue_spin = self->ul_queue_spin;
605 	ulwp->ul_door_noreserve = self->ul_door_noreserve;
606 
607 	/* new thread inherits creating thread's scheduling parameters */
608 	ulwp->ul_policy = self->ul_policy;
609 	ulwp->ul_pri = (self->ul_epri? self->ul_epri : self->ul_pri);
610 	ulwp->ul_cid = self->ul_cid;
611 	ulwp->ul_rtclassid = self->ul_rtclassid;
612 
613 	ulwp->ul_primarymap = self->ul_primarymap;
614 	ulwp->ul_self = ulwp;
615 	ulwp->ul_uberdata = udp;
616 
617 	/* debugger support */
618 	ulwp->ul_usropts = flags;
619 
620 #ifdef __sparc
621 	/*
622 	 * We cache several instructions in the thread structure for use
623 	 * by the fasttrap DTrace provider. When changing this, read the
624 	 * comment in fasttrap.h for the all the other places that must
625 	 * be changed.
626 	 */
627 	ulwp->ul_dsave = 0x9de04000;	/* save %g1, %g0, %sp */
628 	ulwp->ul_drestore = 0x81e80000;	/* restore %g0, %g0, %g0 */
629 	ulwp->ul_dftret = 0x91d0203a;	/* ta 0x3a */
630 	ulwp->ul_dreturn = 0x81ca0000;	/* return %o0 */
631 #endif
632 
633 	ulwp->ul_startpc = func;
634 	ulwp->ul_startarg = arg;
635 	_fpinherit(ulwp);
636 	/*
637 	 * Defer signals on the new thread until its TLS constructors
638 	 * have been called.  _thr_setup() will call sigon() after
639 	 * it has called tls_setup().
640 	 */
641 	ulwp->ul_sigdefer = 1;
642 
643 	if (setup_context(&uc, _thr_setup, ulwp,
644 	    (caddr_t)ulwp->ul_stk + ulwp->ul_guardsize, stksize) != 0)
645 		error = EAGAIN;
646 
647 	/*
648 	 * Call enter_critical() to avoid being suspended until we
649 	 * have linked the new thread into the proper lists.
650 	 * This is necessary because forkall() and fork1() must
651 	 * suspend all threads and they must see a complete list.
652 	 */
653 	enter_critical(self);
654 	uc.uc_sigmask = ulwp->ul_sigmask = self->ul_sigmask;
655 	if (error != 0 ||
656 	    (error = __lwp_create(&uc, lwp_flags, &tid)) != 0) {
657 		exit_critical(self);
658 		ulwp->ul_lwpid = (lwpid_t)(-1);
659 		ulwp->ul_dead = 1;
660 		ulwp->ul_detached = 1;
661 		lmutex_lock(&udp->link_lock);
662 		ulwp_free(ulwp);
663 		lmutex_unlock(&udp->link_lock);
664 		return (error);
665 	}
666 	self->ul_nocancel = 0;	/* cancellation is now possible */
667 	udp->uberflags.uf_mt = 1;
668 	if (new_thread)
669 		*new_thread = tid;
670 	if (flags & THR_DETACHED)
671 		ulwp->ul_detached = 1;
672 	ulwp->ul_lwpid = tid;
673 	ulwp->ul_stop = TSTP_REGULAR;
674 	if (flags & THR_SUSPENDED)
675 		ulwp->ul_created = 1;
676 
677 	lmutex_lock(&udp->link_lock);
678 	ulwp->ul_forw = udp->all_lwps;
679 	ulwp->ul_back = udp->all_lwps->ul_back;
680 	ulwp->ul_back->ul_forw = ulwp;
681 	ulwp->ul_forw->ul_back = ulwp;
682 	hash_in(ulwp, udp);
683 	udp->nthreads++;
684 	if (flags & THR_DAEMON)
685 		udp->ndaemons++;
686 	if (flags & THR_NEW_LWP)
687 		thr_concurrency++;
688 	__libc_threaded = 1;		/* inform stdio */
689 	lmutex_unlock(&udp->link_lock);
690 
691 	if (__td_event_report(self, TD_CREATE, udp)) {
692 		self->ul_td_evbuf.eventnum = TD_CREATE;
693 		self->ul_td_evbuf.eventdata = (void *)(uintptr_t)tid;
694 		tdb_event(TD_CREATE, udp);
695 	}
696 
697 	exit_critical(self);
698 
699 	if (!(flags & THR_SUSPENDED))
700 		(void) _thrp_continue(tid, TSTP_REGULAR);
701 
702 	return (0);
703 }
704 
705 #pragma weak thr_create = _thr_create
706 int
707 _thr_create(void *stk, size_t stksize, void *(*func)(void *), void *arg,
708 	long flags, thread_t *new_thread)
709 {
710 	return (_thrp_create(stk, stksize, func, arg, flags, new_thread, 0));
711 }
712 
713 /*
714  * A special cancellation cleanup hook for DCE.
715  * cleanuphndlr, when it is not NULL, will contain a callback
716  * function to be called before a thread is terminated in
717  * _thr_exit() as a result of being cancelled.
718  */
719 static void (*cleanuphndlr)(void) = NULL;
720 
721 /*
722  * _pthread_setcleanupinit: sets the cleanup hook.
723  */
724 int
725 _pthread_setcleanupinit(void (*func)(void))
726 {
727 	cleanuphndlr = func;
728 	return (0);
729 }
730 
731 void
732 _thrp_exit()
733 {
734 	ulwp_t *self = curthread;
735 	uberdata_t *udp = self->ul_uberdata;
736 	ulwp_t *replace = NULL;
737 
738 	if (__td_event_report(self, TD_DEATH, udp)) {
739 		self->ul_td_evbuf.eventnum = TD_DEATH;
740 		tdb_event(TD_DEATH, udp);
741 	}
742 
743 	ASSERT(self->ul_sigdefer != 0);
744 
745 	lmutex_lock(&udp->link_lock);
746 	udp->nthreads--;
747 	if (self->ul_usropts & THR_NEW_LWP)
748 		thr_concurrency--;
749 	if (self->ul_usropts & THR_DAEMON)
750 		udp->ndaemons--;
751 	else if (udp->nthreads == udp->ndaemons) {
752 		/*
753 		 * We are the last non-daemon thread exiting.
754 		 * Exit the process.  We retain our TSD and TLS so
755 		 * that atexit() application functions can use them.
756 		 */
757 		lmutex_unlock(&udp->link_lock);
758 		exit(0);
759 		thr_panic("_thrp_exit(): exit(0) returned");
760 	}
761 	lmutex_unlock(&udp->link_lock);
762 
763 	tsd_exit();		/* deallocate thread-specific data */
764 	tls_exit();		/* deallocate thread-local storage */
765 	heldlock_exit();	/* deal with left-over held locks */
766 
767 	/* block all signals to finish exiting */
768 	block_all_signals(self);
769 	/* also prevent ourself from being suspended */
770 	enter_critical(self);
771 	rwl_free(self);
772 	lmutex_lock(&udp->link_lock);
773 	ulwp_free(self);
774 	(void) ulwp_lock(self, udp);
775 
776 	if (self->ul_mapsiz && !self->ul_detached) {
777 		/*
778 		 * We want to free the stack for reuse but must keep
779 		 * the ulwp_t struct for the benefit of thr_join().
780 		 * For this purpose we allocate a replacement ulwp_t.
781 		 */
782 		if ((replace = udp->ulwp_replace_free) == NULL)
783 			replace = lmalloc(REPLACEMENT_SIZE);
784 		else if ((udp->ulwp_replace_free = replace->ul_next) == NULL)
785 			udp->ulwp_replace_last = NULL;
786 	}
787 
788 	if (udp->all_lwps == self)
789 		udp->all_lwps = self->ul_forw;
790 	if (udp->all_lwps == self)
791 		udp->all_lwps = NULL;
792 	else {
793 		self->ul_forw->ul_back = self->ul_back;
794 		self->ul_back->ul_forw = self->ul_forw;
795 	}
796 	self->ul_forw = self->ul_back = NULL;
797 #if defined(THREAD_DEBUG)
798 	/* collect queue lock statistics before marking ourself dead */
799 	record_spin_locks(self);
800 #endif
801 	self->ul_dead = 1;
802 	self->ul_pleasestop = 0;
803 	if (replace != NULL) {
804 		int ix = self->ul_ix;		/* the hash index */
805 		(void) memcpy(replace, self, REPLACEMENT_SIZE);
806 		replace->ul_self = replace;
807 		replace->ul_next = NULL;	/* clone not on stack list */
808 		replace->ul_mapsiz = 0;		/* allows clone to be freed */
809 		replace->ul_replace = 1;	/* requires clone to be freed */
810 		hash_out_unlocked(self, ix, udp);
811 		hash_in_unlocked(replace, ix, udp);
812 		ASSERT(!(self->ul_detached));
813 		self->ul_detached = 1;		/* this frees the stack */
814 		self->ul_schedctl = NULL;
815 		self->ul_schedctl_called = &udp->uberflags;
816 		set_curthread(self = replace);
817 		/*
818 		 * Having just changed the address of curthread, we
819 		 * must reset the ownership of the locks we hold so
820 		 * that assertions will not fire when we release them.
821 		 */
822 		udp->link_lock.mutex_owner = (uintptr_t)self;
823 		ulwp_mutex(self, udp)->mutex_owner = (uintptr_t)self;
824 		/*
825 		 * NOTE:
826 		 * On i386, %gs still references the original, not the
827 		 * replacement, ulwp structure.  Fetching the replacement
828 		 * curthread pointer via %gs:0 works correctly since the
829 		 * original ulwp structure will not be reallocated until
830 		 * this lwp has completed its lwp_exit() system call (see
831 		 * dead_and_buried()), but from here on out, we must make
832 		 * no references to %gs:<offset> other than %gs:0.
833 		 */
834 	}
835 	/*
836 	 * Put non-detached terminated threads in the all_zombies list.
837 	 */
838 	if (!self->ul_detached) {
839 		udp->nzombies++;
840 		if (udp->all_zombies == NULL) {
841 			ASSERT(udp->nzombies == 1);
842 			udp->all_zombies = self->ul_forw = self->ul_back = self;
843 		} else {
844 			self->ul_forw = udp->all_zombies;
845 			self->ul_back = udp->all_zombies->ul_back;
846 			self->ul_back->ul_forw = self;
847 			self->ul_forw->ul_back = self;
848 		}
849 	}
850 	/*
851 	 * Notify everyone waiting for this thread.
852 	 */
853 	ulwp_broadcast(self);
854 	(void) ulwp_unlock(self, udp);
855 	/*
856 	 * Prevent any more references to the schedctl data.
857 	 * We are exiting and continue_fork() may not find us.
858 	 * Do this just before dropping link_lock, since fork
859 	 * serializes on link_lock.
860 	 */
861 	self->ul_schedctl = NULL;
862 	self->ul_schedctl_called = &udp->uberflags;
863 	lmutex_unlock(&udp->link_lock);
864 
865 	ASSERT(self->ul_critical == 1);
866 	ASSERT(self->ul_preempt == 0);
867 	_lwp_terminate();	/* never returns */
868 	thr_panic("_thrp_exit(): _lwp_terminate() returned");
869 }
870 
871 #if defined(THREAD_DEBUG)
872 void
873 collect_queue_statistics()
874 {
875 	uberdata_t *udp = curthread->ul_uberdata;
876 	ulwp_t *ulwp;
877 
878 	if (thread_queue_dump) {
879 		lmutex_lock(&udp->link_lock);
880 		if ((ulwp = udp->all_lwps) != NULL) {
881 			do {
882 				record_spin_locks(ulwp);
883 			} while ((ulwp = ulwp->ul_forw) != udp->all_lwps);
884 		}
885 		lmutex_unlock(&udp->link_lock);
886 	}
887 }
888 #endif
889 
890 void
891 _thr_exit_common(void *status, int unwind)
892 {
893 	ulwp_t *self = curthread;
894 	int cancelled = (self->ul_cancel_pending && status == PTHREAD_CANCELED);
895 
896 	ASSERT(self->ul_critical == 0 && self->ul_preempt == 0);
897 
898 	/*
899 	 * Disable cancellation and call the special DCE cancellation
900 	 * cleanup hook if it is enabled.  Do nothing else before calling
901 	 * the DCE cancellation cleanup hook; it may call longjmp() and
902 	 * never return here.
903 	 */
904 	self->ul_cancel_disabled = 1;
905 	self->ul_cancel_async = 0;
906 	self->ul_save_async = 0;
907 	self->ul_cancelable = 0;
908 	self->ul_cancel_pending = 0;
909 	set_cancel_pending_flag(self, 1);
910 	if (cancelled && cleanuphndlr != NULL)
911 		(*cleanuphndlr)();
912 
913 	/*
914 	 * Block application signals while we are exiting.
915 	 * We call out to C++, TSD, and TLS destructors while exiting
916 	 * and these are application-defined, so we cannot be assured
917 	 * that they won't reset the signal mask.  We use sigoff() to
918 	 * defer any signals that may be received as a result of this
919 	 * bad behavior.  Such signals will be lost to the process
920 	 * when the thread finishes exiting.
921 	 */
922 	(void) _thr_sigsetmask(SIG_SETMASK, &maskset, NULL);
923 	sigoff(self);
924 
925 	self->ul_rval = status;
926 
927 	/*
928 	 * If thr_exit is being called from the places where
929 	 * C++ destructors are to be called such as cancellation
930 	 * points, then set this flag. It is checked in _t_cancel()
931 	 * to decide whether _ex_unwind() is to be called or not.
932 	 */
933 	if (unwind)
934 		self->ul_unwind = 1;
935 
936 	/*
937 	 * _thrp_unwind() will eventually call _thrp_exit().
938 	 * It never returns.
939 	 */
940 	_thrp_unwind(NULL);
941 	thr_panic("_thr_exit_common(): _thrp_unwind() returned");
942 }
943 
944 /*
945  * Called when a thread returns from its start function.
946  * We are at the top of the stack; no unwinding is necessary.
947  */
948 void
949 _thr_terminate(void *status)
950 {
951 	_thr_exit_common(status, 0);
952 }
953 
954 #pragma weak thr_exit = _thr_exit
955 #pragma weak pthread_exit = _thr_exit
956 #pragma weak _pthread_exit = _thr_exit
957 void
958 _thr_exit(void *status)
959 {
960 	_thr_exit_common(status, 1);
961 }
962 
963 int
964 _thrp_join(thread_t tid, thread_t *departed, void **status, int do_cancel)
965 {
966 	uberdata_t *udp = curthread->ul_uberdata;
967 	mutex_t *mp;
968 	void *rval;
969 	thread_t found;
970 	ulwp_t *ulwp;
971 	ulwp_t **ulwpp;
972 	int replace;
973 	int error;
974 
975 	if (do_cancel)
976 		error = lwp_wait(tid, &found);
977 	else {
978 		while ((error = __lwp_wait(tid, &found)) == EINTR)
979 			;
980 	}
981 	if (error)
982 		return (error);
983 
984 	/*
985 	 * We must hold link_lock to avoid a race condition with find_stack().
986 	 */
987 	lmutex_lock(&udp->link_lock);
988 	if ((ulwpp = find_lwpp(found)) == NULL) {
989 		/*
990 		 * lwp_wait() found an lwp that the library doesn't know
991 		 * about.  It must have been created with _lwp_create().
992 		 * Just return its lwpid; we can't know its status.
993 		 */
994 		lmutex_unlock(&udp->link_lock);
995 		rval = NULL;
996 	} else {
997 		/*
998 		 * Remove ulwp from the hash table.
999 		 */
1000 		ulwp = *ulwpp;
1001 		*ulwpp = ulwp->ul_hash;
1002 		ulwp->ul_hash = NULL;
1003 		/*
1004 		 * Remove ulwp from all_zombies list.
1005 		 */
1006 		ASSERT(udp->nzombies >= 1);
1007 		if (udp->all_zombies == ulwp)
1008 			udp->all_zombies = ulwp->ul_forw;
1009 		if (udp->all_zombies == ulwp)
1010 			udp->all_zombies = NULL;
1011 		else {
1012 			ulwp->ul_forw->ul_back = ulwp->ul_back;
1013 			ulwp->ul_back->ul_forw = ulwp->ul_forw;
1014 		}
1015 		ulwp->ul_forw = ulwp->ul_back = NULL;
1016 		udp->nzombies--;
1017 		ASSERT(ulwp->ul_dead && !ulwp->ul_detached &&
1018 		    !(ulwp->ul_usropts & (THR_DETACHED|THR_DAEMON)));
1019 		/*
1020 		 * We can't call ulwp_unlock(ulwp) after we set
1021 		 * ulwp->ul_ix = -1 so we have to get a pointer to the
1022 		 * ulwp's hash table mutex now in order to unlock it below.
1023 		 */
1024 		mp = ulwp_mutex(ulwp, udp);
1025 		ulwp->ul_lwpid = (lwpid_t)(-1);
1026 		ulwp->ul_ix = -1;
1027 		rval = ulwp->ul_rval;
1028 		replace = ulwp->ul_replace;
1029 		lmutex_unlock(mp);
1030 		if (replace) {
1031 			ulwp->ul_next = NULL;
1032 			if (udp->ulwp_replace_free == NULL)
1033 				udp->ulwp_replace_free =
1034 				    udp->ulwp_replace_last = ulwp;
1035 			else {
1036 				udp->ulwp_replace_last->ul_next = ulwp;
1037 				udp->ulwp_replace_last = ulwp;
1038 			}
1039 		}
1040 		lmutex_unlock(&udp->link_lock);
1041 	}
1042 
1043 	if (departed != NULL)
1044 		*departed = found;
1045 	if (status != NULL)
1046 		*status = rval;
1047 	return (0);
1048 }
1049 
1050 #pragma weak thr_join = _thr_join
1051 int
1052 _thr_join(thread_t tid, thread_t *departed, void **status)
1053 {
1054 	int error = _thrp_join(tid, departed, status, 1);
1055 	return ((error == EINVAL)? ESRCH : error);
1056 }
1057 
1058 /*
1059  * pthread_join() differs from Solaris thr_join():
1060  * It does not return the departed thread's id
1061  * and hence does not have a "departed" argument.
1062  * It returns EINVAL if tid refers to a detached thread.
1063  */
1064 #pragma weak pthread_join = _pthread_join
1065 int
1066 _pthread_join(pthread_t tid, void **status)
1067 {
1068 	return ((tid == 0)? ESRCH : _thrp_join(tid, NULL, status, 1));
1069 }
1070 
1071 #pragma weak pthread_detach = _thr_detach
1072 #pragma weak _pthread_detach = _thr_detach
1073 int
1074 _thr_detach(thread_t tid)
1075 {
1076 	uberdata_t *udp = curthread->ul_uberdata;
1077 	ulwp_t *ulwp;
1078 	ulwp_t **ulwpp;
1079 	int error = 0;
1080 
1081 	if ((ulwpp = find_lwpp(tid)) == NULL)
1082 		return (ESRCH);
1083 	ulwp = *ulwpp;
1084 
1085 	if (ulwp->ul_dead) {
1086 		ulwp_unlock(ulwp, udp);
1087 		error = _thrp_join(tid, NULL, NULL, 0);
1088 	} else {
1089 		error = __lwp_detach(tid);
1090 		ulwp->ul_detached = 1;
1091 		ulwp->ul_usropts |= THR_DETACHED;
1092 		ulwp_unlock(ulwp, udp);
1093 	}
1094 	return (error);
1095 }
1096 
1097 /*
1098  * Static local string compare function to avoid calling strncmp()
1099  * (and hence the dynamic linker) during library initialization.
1100  */
1101 static int
1102 sncmp(const char *s1, const char *s2, size_t n)
1103 {
1104 	n++;
1105 	while (--n != 0 && *s1 == *s2++)
1106 		if (*s1++ == '\0')
1107 			return (0);
1108 	return (n == 0 ? 0 : *(uchar_t *)s1 - *(uchar_t *)--s2);
1109 }
1110 
1111 static const char *
1112 ematch(const char *ev, const char *match)
1113 {
1114 	int c;
1115 
1116 	while ((c = *match++) != '\0') {
1117 		if (*ev++ != c)
1118 			return (NULL);
1119 	}
1120 	if (*ev++ != '=')
1121 		return (NULL);
1122 	return (ev);
1123 }
1124 
1125 static int
1126 envvar(const char *ev, const char *match, int limit)
1127 {
1128 	int val = -1;
1129 	const char *ename;
1130 
1131 	if ((ename = ematch(ev, match)) != NULL) {
1132 		int c;
1133 		for (val = 0; (c = *ename) != '\0'; ename++) {
1134 			if (!isdigit(c)) {
1135 				val = -1;
1136 				break;
1137 			}
1138 			val = val * 10 + (c - '0');
1139 			if (val > limit) {
1140 				val = limit;
1141 				break;
1142 			}
1143 		}
1144 	}
1145 	return (val);
1146 }
1147 
1148 static void
1149 etest(const char *ev)
1150 {
1151 	int value;
1152 
1153 	if ((value = envvar(ev, "QUEUE_SPIN", 1000000)) >= 0)
1154 		thread_queue_spin = value;
1155 	if ((value = envvar(ev, "ADAPTIVE_SPIN", 1000000)) >= 0)
1156 		thread_adaptive_spin = value;
1157 	if ((value = envvar(ev, "MAX_SPINNERS", 255)) >= 0)
1158 		thread_max_spinners = value;
1159 	if ((value = envvar(ev, "QUEUE_FIFO", 8)) >= 0)
1160 		thread_queue_fifo = value;
1161 #if defined(THREAD_DEBUG)
1162 	if ((value = envvar(ev, "QUEUE_VERIFY", 1)) >= 0)
1163 		thread_queue_verify = value;
1164 	if ((value = envvar(ev, "QUEUE_DUMP", 1)) >= 0)
1165 		thread_queue_dump = value;
1166 #endif
1167 	if ((value = envvar(ev, "STACK_CACHE", 10000)) >= 0)
1168 		thread_stack_cache = value;
1169 	if ((value = envvar(ev, "COND_WAIT_DEFER", 1)) >= 0)
1170 		thread_cond_wait_defer = value;
1171 	if ((value = envvar(ev, "ERROR_DETECTION", 2)) >= 0)
1172 		thread_error_detection = value;
1173 	if ((value = envvar(ev, "ASYNC_SAFE", 1)) >= 0)
1174 		thread_async_safe = value;
1175 	if ((value = envvar(ev, "DOOR_NORESERVE", 1)) >= 0)
1176 		thread_door_noreserve = value;
1177 }
1178 
1179 /*
1180  * Look for and evaluate environment variables of the form "_THREAD_*".
1181  * For compatibility with the past, we also look for environment
1182  * names of the form "LIBTHREAD_*".
1183  */
1184 static void
1185 set_thread_vars()
1186 {
1187 	extern const char **_environ;
1188 	const char **pev;
1189 	const char *ev;
1190 	char c;
1191 
1192 	if ((pev = _environ) == NULL)
1193 		return;
1194 	while ((ev = *pev++) != NULL) {
1195 		c = *ev;
1196 		if (c == '_' && sncmp(ev, "_THREAD_", 8) == 0)
1197 			etest(ev + 8);
1198 		if (c == 'L' && sncmp(ev, "LIBTHREAD_", 10) == 0)
1199 			etest(ev + 10);
1200 	}
1201 }
1202 
1203 /* PROBE_SUPPORT begin */
1204 #pragma weak __tnf_probe_notify
1205 extern void __tnf_probe_notify(void);
1206 /* PROBE_SUPPORT end */
1207 
1208 /* same as atexit() but private to the library */
1209 extern int _atexit(void (*)(void));
1210 
1211 /* same as _cleanup() but private to the library */
1212 extern void __cleanup(void);
1213 
1214 extern void atfork_init(void);
1215 
1216 #ifdef __amd64
1217 extern void __proc64id(void);
1218 #endif
1219 
1220 /*
1221  * libc_init() is called by ld.so.1 for library initialization.
1222  * We perform minimal initialization; enough to work with the main thread.
1223  */
1224 void
1225 libc_init(void)
1226 {
1227 	uberdata_t *udp = &__uberdata;
1228 	ulwp_t *oldself = __curthread();
1229 	ucontext_t uc;
1230 	ulwp_t *self;
1231 	struct rlimit rl;
1232 	caddr_t data;
1233 	size_t tls_size;
1234 	int setmask;
1235 
1236 	/*
1237 	 * For the initial stage of initialization, we must be careful
1238 	 * not to call any function that could possibly call _cerror().
1239 	 * For this purpose, we call only the raw system call wrappers.
1240 	 */
1241 
1242 #ifdef __amd64
1243 	/*
1244 	 * Gather information about cache layouts for optimized
1245 	 * AMD and Intel assembler strfoo() and memfoo() functions.
1246 	 */
1247 	__proc64id();
1248 #endif
1249 
1250 	/*
1251 	 * Every libc, regardless of which link map, must register __cleanup().
1252 	 */
1253 	(void) _atexit(__cleanup);
1254 
1255 	/*
1256 	 * We keep our uberdata on one of (a) the first alternate link map
1257 	 * or (b) the primary link map.  We switch to the primary link map
1258 	 * and stay there once we see it.  All intermediate link maps are
1259 	 * subject to being unloaded at any time.
1260 	 */
1261 	if (oldself != NULL && (oldself->ul_primarymap || !primary_link_map)) {
1262 		__tdb_bootstrap = oldself->ul_uberdata->tdb_bootstrap;
1263 		mutex_setup();
1264 		atfork_init();	/* every link map needs atfork() processing */
1265 		return;
1266 	}
1267 
1268 	/*
1269 	 * To establish the main stack information, we have to get our context.
1270 	 * This is also convenient to use for getting our signal mask.
1271 	 */
1272 	uc.uc_flags = UC_ALL;
1273 	(void) __getcontext(&uc);
1274 	ASSERT(uc.uc_link == NULL);
1275 
1276 	tls_size = roundup64(udp->tls_metadata.static_tls.tls_size);
1277 	ASSERT(primary_link_map || tls_size == 0);
1278 	data = lmalloc(sizeof (ulwp_t) + tls_size);
1279 	if (data == NULL)
1280 		thr_panic("cannot allocate thread structure for main thread");
1281 	/* LINTED pointer cast may result in improper alignment */
1282 	self = (ulwp_t *)(data + tls_size);
1283 	init_hash_table[0].hash_bucket = self;
1284 
1285 	self->ul_sigmask = uc.uc_sigmask;
1286 	delete_reserved_signals(&self->ul_sigmask);
1287 	/*
1288 	 * Are the old and new sets different?
1289 	 * (This can happen if we are currently blocking SIGCANCEL.)
1290 	 * If so, we must explicitly set our signal mask, below.
1291 	 */
1292 	setmask =
1293 	    ((self->ul_sigmask.__sigbits[0] ^ uc.uc_sigmask.__sigbits[0]) |
1294 	    (self->ul_sigmask.__sigbits[1] ^ uc.uc_sigmask.__sigbits[1]));
1295 
1296 #ifdef __sparc
1297 	/*
1298 	 * We cache several instructions in the thread structure for use
1299 	 * by the fasttrap DTrace provider. When changing this, read the
1300 	 * comment in fasttrap.h for the all the other places that must
1301 	 * be changed.
1302 	 */
1303 	self->ul_dsave = 0x9de04000;	/* save %g1, %g0, %sp */
1304 	self->ul_drestore = 0x81e80000;	/* restore %g0, %g0, %g0 */
1305 	self->ul_dftret = 0x91d0203a;	/* ta 0x3a */
1306 	self->ul_dreturn = 0x81ca0000;	/* return %o0 */
1307 #endif
1308 
1309 	self->ul_stktop = (uintptr_t)uc.uc_stack.ss_sp + uc.uc_stack.ss_size;
1310 	(void) getrlimit(RLIMIT_STACK, &rl);
1311 	self->ul_stksiz = rl.rlim_cur;
1312 	self->ul_stk = (caddr_t)(self->ul_stktop - self->ul_stksiz);
1313 
1314 	self->ul_forw = self->ul_back = self;
1315 	self->ul_hash = NULL;
1316 	self->ul_ix = 0;
1317 	self->ul_lwpid = 1; /* __lwp_self() */
1318 	self->ul_main = 1;
1319 	self->ul_self = self;
1320 	self->ul_policy = -1;		/* initialize only when needed */
1321 	self->ul_pri = 0;
1322 	self->ul_cid = 0;
1323 	self->ul_rtclassid = -1;
1324 	self->ul_uberdata = udp;
1325 	if (oldself != NULL) {
1326 		int i;
1327 
1328 		ASSERT(primary_link_map);
1329 		ASSERT(oldself->ul_main == 1);
1330 		self->ul_stsd = oldself->ul_stsd;
1331 		for (i = 0; i < TSD_NFAST; i++)
1332 			self->ul_ftsd[i] = oldself->ul_ftsd[i];
1333 		self->ul_tls = oldself->ul_tls;
1334 		/*
1335 		 * Retrieve all pointers to uberdata allocated
1336 		 * while running on previous link maps.
1337 		 * We would like to do a structure assignment here, but
1338 		 * gcc turns structure assignments into calls to memcpy(),
1339 		 * a function exported from libc.  We can't call any such
1340 		 * external functions until we establish curthread, below,
1341 		 * so we just call our private version of memcpy().
1342 		 */
1343 		(void) memcpy(udp, oldself->ul_uberdata, sizeof (*udp));
1344 		/*
1345 		 * These items point to global data on the primary link map.
1346 		 */
1347 		udp->thr_hash_table = init_hash_table;
1348 		udp->sigacthandler = sigacthandler;
1349 		udp->tdb.tdb_events = tdb_events;
1350 		ASSERT(udp->nthreads == 1 && !udp->uberflags.uf_mt);
1351 		ASSERT(udp->lwp_stacks == NULL);
1352 		ASSERT(udp->ulwp_freelist == NULL);
1353 		ASSERT(udp->ulwp_replace_free == NULL);
1354 		ASSERT(udp->hash_size == 1);
1355 	}
1356 	udp->all_lwps = self;
1357 	udp->ulwp_one = self;
1358 	udp->pid = getpid();
1359 	udp->nthreads = 1;
1360 	/*
1361 	 * In every link map, tdb_bootstrap points to the same piece of
1362 	 * allocated memory.  When the primary link map is initialized,
1363 	 * the allocated memory is assigned a pointer to the one true
1364 	 * uberdata.  This allows libc_db to initialize itself regardless
1365 	 * of which instance of libc it finds in the address space.
1366 	 */
1367 	if (udp->tdb_bootstrap == NULL)
1368 		udp->tdb_bootstrap = lmalloc(sizeof (uberdata_t *));
1369 	__tdb_bootstrap = udp->tdb_bootstrap;
1370 	if (primary_link_map) {
1371 		self->ul_primarymap = 1;
1372 		udp->primary_map = 1;
1373 		*udp->tdb_bootstrap = udp;
1374 	}
1375 	/*
1376 	 * Cancellation can't happen until:
1377 	 *	pthread_cancel() is called
1378 	 * or:
1379 	 *	another thread is created
1380 	 * For now, as a single-threaded process, set the flag that tells
1381 	 * PROLOGUE/EPILOGUE (in scalls.c) that cancellation can't happen.
1382 	 */
1383 	self->ul_nocancel = 1;
1384 
1385 #if defined(__amd64)
1386 	(void) ___lwp_private(_LWP_SETPRIVATE, _LWP_FSBASE, self);
1387 #elif defined(__i386)
1388 	(void) ___lwp_private(_LWP_SETPRIVATE, _LWP_GSBASE, self);
1389 #endif	/* __i386 || __amd64 */
1390 	set_curthread(self);		/* redundant on i386 */
1391 	/*
1392 	 * Now curthread is established and it is safe to call any
1393 	 * function in libc except one that uses thread-local storage.
1394 	 */
1395 	self->ul_errnop = &errno;
1396 	if (oldself != NULL) {
1397 		/* tls_size was zero when oldself was allocated */
1398 		lfree(oldself, sizeof (ulwp_t));
1399 	}
1400 	mutex_setup();
1401 	atfork_init();
1402 	signal_init();
1403 
1404 	/*
1405 	 * If the stack is unlimited, we set the size to zero to disable
1406 	 * stack checking.
1407 	 * XXX: Work harder here.  Get the stack size from /proc/self/rmap
1408 	 */
1409 	if (self->ul_stksiz == RLIM_INFINITY) {
1410 		self->ul_ustack.ss_sp = (void *)self->ul_stktop;
1411 		self->ul_ustack.ss_size = 0;
1412 	} else {
1413 		self->ul_ustack.ss_sp = self->ul_stk;
1414 		self->ul_ustack.ss_size = self->ul_stksiz;
1415 	}
1416 	self->ul_ustack.ss_flags = 0;
1417 	(void) setustack(&self->ul_ustack);
1418 
1419 	/*
1420 	 * Get the variables that affect thread behavior from the environment.
1421 	 */
1422 	set_thread_vars();
1423 	udp->uberflags.uf_thread_error_detection = (char)thread_error_detection;
1424 	udp->thread_stack_cache = thread_stack_cache;
1425 
1426 	/*
1427 	 * Make per-thread copies of global variables, for speed.
1428 	 */
1429 	self->ul_queue_fifo = (char)thread_queue_fifo;
1430 	self->ul_cond_wait_defer = (char)thread_cond_wait_defer;
1431 	self->ul_error_detection = (char)thread_error_detection;
1432 	self->ul_async_safe = (char)thread_async_safe;
1433 	self->ul_door_noreserve = (char)thread_door_noreserve;
1434 	self->ul_max_spinners = (uint8_t)thread_max_spinners;
1435 	self->ul_adaptive_spin = thread_adaptive_spin;
1436 	self->ul_queue_spin = thread_queue_spin;
1437 
1438 	/*
1439 	 * When we have initialized the primary link map, inform
1440 	 * the dynamic linker about our interface functions.
1441 	 */
1442 	if (self->ul_primarymap)
1443 		_ld_libc((void *)rtld_funcs);
1444 
1445 	/*
1446 	 * Defer signals until TLS constructors have been called.
1447 	 */
1448 	sigoff(self);
1449 	tls_setup();
1450 	sigon(self);
1451 	if (setmask)
1452 		(void) restore_signals(self);
1453 
1454 	/*
1455 	 * Make private copies of __xpg4 and __xpg6 so libc can test
1456 	 * them after this point without invoking the dynamic linker.
1457 	 */
1458 	libc__xpg4 = __xpg4;
1459 	libc__xpg6 = __xpg6;
1460 
1461 	/* PROBE_SUPPORT begin */
1462 	if (self->ul_primarymap && __tnf_probe_notify != NULL)
1463 		__tnf_probe_notify();
1464 	/* PROBE_SUPPORT end */
1465 
1466 	init_sigev_thread();
1467 	init_aio();
1468 
1469 	/*
1470 	 * We need to reset __threaded dynamically at runtime, so that
1471 	 * __threaded can be bound to __threaded outside libc which may not
1472 	 * have initial value of 1 (without a copy relocation in a.out).
1473 	 */
1474 	__threaded = 1;
1475 }
1476 
1477 #pragma fini(libc_fini)
1478 void
1479 libc_fini()
1480 {
1481 	/*
1482 	 * If we are doing fini processing for the instance of libc
1483 	 * on the first alternate link map (this happens only when
1484 	 * the dynamic linker rejects a bad audit library), then clear
1485 	 * __curthread().  We abandon whatever memory was allocated by
1486 	 * lmalloc() while running on this alternate link-map but we
1487 	 * don't care (and can't find the memory in any case); we just
1488 	 * want to protect the application from this bad audit library.
1489 	 * No fini processing is done by libc in the normal case.
1490 	 */
1491 
1492 	uberdata_t *udp = curthread->ul_uberdata;
1493 
1494 	if (udp->primary_map == 0 && udp == &__uberdata)
1495 		set_curthread(NULL);
1496 }
1497 
1498 /*
1499  * finish_init is called when we are about to become multi-threaded,
1500  * that is, on the first call to thr_create().
1501  */
1502 void
1503 finish_init()
1504 {
1505 	ulwp_t *self = curthread;
1506 	uberdata_t *udp = self->ul_uberdata;
1507 	thr_hash_table_t *htp;
1508 	void *data;
1509 	int i;
1510 
1511 	/*
1512 	 * No locks needed here; we are single-threaded on the first call.
1513 	 * We can be called only after the primary link map has been set up.
1514 	 */
1515 	ASSERT(self->ul_primarymap);
1516 	ASSERT(self == udp->ulwp_one);
1517 	ASSERT(!udp->uberflags.uf_mt);
1518 	ASSERT(udp->hash_size == 1);
1519 
1520 	/*
1521 	 * Initialize self->ul_policy, self->ul_cid, and self->ul_pri.
1522 	 */
1523 	update_sched(self);
1524 
1525 	/*
1526 	 * Allocate the queue_head array if not already allocated.
1527 	 */
1528 	if (udp->queue_head == NULL)
1529 		queue_alloc();
1530 
1531 	/*
1532 	 * Now allocate the thread hash table.
1533 	 */
1534 	if ((data = mmap(NULL, HASHTBLSZ * sizeof (thr_hash_table_t),
1535 	    PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, (off_t)0))
1536 	    == MAP_FAILED)
1537 		thr_panic("cannot allocate thread hash table");
1538 
1539 	udp->thr_hash_table = htp = (thr_hash_table_t *)data;
1540 	udp->hash_size = HASHTBLSZ;
1541 	udp->hash_mask = HASHTBLSZ - 1;
1542 
1543 	for (i = 0; i < HASHTBLSZ; i++, htp++) {
1544 		htp->hash_lock.mutex_flag = LOCK_INITED;
1545 		htp->hash_lock.mutex_magic = MUTEX_MAGIC;
1546 		htp->hash_cond.cond_magic = COND_MAGIC;
1547 	}
1548 	hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
1549 
1550 	/*
1551 	 * Set up the SIGCANCEL handler for threads cancellation.
1552 	 */
1553 	setup_cancelsig(SIGCANCEL);
1554 
1555 	/*
1556 	 * Arrange to do special things on exit --
1557 	 * - collect queue statistics from all remaining active threads.
1558 	 * - dump queue statistics to stderr if _THREAD_QUEUE_DUMP is set.
1559 	 * - grab assert_lock to ensure that assertion failures
1560 	 *   and a core dump take precedence over _exit().
1561 	 * (Functions are called in the reverse order of their registration.)
1562 	 */
1563 	(void) _atexit(grab_assert_lock);
1564 #if defined(THREAD_DEBUG)
1565 	(void) _atexit(dump_queue_statistics);
1566 	(void) _atexit(collect_queue_statistics);
1567 #endif
1568 }
1569 
1570 /*
1571  * Used only by postfork1_child(), below.
1572  */
1573 static void
1574 mark_dead_and_buried(ulwp_t *ulwp)
1575 {
1576 	ulwp->ul_dead = 1;
1577 	ulwp->ul_lwpid = (lwpid_t)(-1);
1578 	ulwp->ul_hash = NULL;
1579 	ulwp->ul_ix = -1;
1580 	ulwp->ul_schedctl = NULL;
1581 	ulwp->ul_schedctl_called = NULL;
1582 }
1583 
1584 /*
1585  * This is called from fork1() in the child.
1586  * Reset our data structures to reflect one lwp.
1587  */
1588 void
1589 postfork1_child()
1590 {
1591 	ulwp_t *self = curthread;
1592 	uberdata_t *udp = self->ul_uberdata;
1593 	queue_head_t *qp;
1594 	ulwp_t *next;
1595 	ulwp_t *ulwp;
1596 	int i;
1597 
1598 	/* daemon threads shouldn't call fork1(), but oh well... */
1599 	self->ul_usropts &= ~THR_DAEMON;
1600 	udp->nthreads = 1;
1601 	udp->ndaemons = 0;
1602 	udp->uberflags.uf_mt = 0;
1603 	__libc_threaded = 0;
1604 	for (i = 0; i < udp->hash_size; i++)
1605 		udp->thr_hash_table[i].hash_bucket = NULL;
1606 	self->ul_lwpid = __lwp_self();
1607 	hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp);
1608 
1609 	/*
1610 	 * Some thread in the parent might have been suspended
1611 	 * while holding udp->callout_lock or udp->ld_lock.
1612 	 * Reinitialize the child's copies.
1613 	 */
1614 	mutex_init(&udp->callout_lock, USYNC_THREAD | LOCK_RECURSIVE, NULL);
1615 	mutex_init(&udp->ld_lock, USYNC_THREAD | LOCK_RECURSIVE, NULL);
1616 
1617 	/* no one in the child is on a sleep queue; reinitialize */
1618 	if ((qp = udp->queue_head) != NULL) {
1619 		(void) memset(qp, 0, 2 * QHASHSIZE * sizeof (queue_head_t));
1620 		for (i = 0; i < 2 * QHASHSIZE; qp++, i++) {
1621 			qp->qh_type = (i < QHASHSIZE)? MX : CV;
1622 			qp->qh_lock.mutex_flag = LOCK_INITED;
1623 			qp->qh_lock.mutex_magic = MUTEX_MAGIC;
1624 			qp->qh_hlist = &qp->qh_def_root;
1625 #if defined(THREAD_DEBUG)
1626 			qp->qh_hlen = 1;
1627 			qp->qh_hmax = 1;
1628 #endif
1629 		}
1630 	}
1631 
1632 	/*
1633 	 * All lwps except ourself are gone.  Mark them so.
1634 	 * First mark all of the lwps that have already been freed.
1635 	 * Then mark and free all of the active lwps except ourself.
1636 	 * Since we are single-threaded, no locks are required here.
1637 	 */
1638 	for (ulwp = udp->lwp_stacks; ulwp != NULL; ulwp = ulwp->ul_next)
1639 		mark_dead_and_buried(ulwp);
1640 	for (ulwp = udp->ulwp_freelist; ulwp != NULL; ulwp = ulwp->ul_next)
1641 		mark_dead_and_buried(ulwp);
1642 	for (ulwp = self->ul_forw; ulwp != self; ulwp = next) {
1643 		next = ulwp->ul_forw;
1644 		ulwp->ul_forw = ulwp->ul_back = NULL;
1645 		mark_dead_and_buried(ulwp);
1646 		tsd_free(ulwp);
1647 		tls_free(ulwp);
1648 		rwl_free(ulwp);
1649 		heldlock_free(ulwp);
1650 		ulwp_free(ulwp);
1651 	}
1652 	self->ul_forw = self->ul_back = udp->all_lwps = self;
1653 	if (self != udp->ulwp_one)
1654 		mark_dead_and_buried(udp->ulwp_one);
1655 	if ((ulwp = udp->all_zombies) != NULL) {
1656 		ASSERT(udp->nzombies != 0);
1657 		do {
1658 			next = ulwp->ul_forw;
1659 			ulwp->ul_forw = ulwp->ul_back = NULL;
1660 			mark_dead_and_buried(ulwp);
1661 			udp->nzombies--;
1662 			if (ulwp->ul_replace) {
1663 				ulwp->ul_next = NULL;
1664 				if (udp->ulwp_replace_free == NULL) {
1665 					udp->ulwp_replace_free =
1666 					    udp->ulwp_replace_last = ulwp;
1667 				} else {
1668 					udp->ulwp_replace_last->ul_next = ulwp;
1669 					udp->ulwp_replace_last = ulwp;
1670 				}
1671 			}
1672 		} while ((ulwp = next) != udp->all_zombies);
1673 		ASSERT(udp->nzombies == 0);
1674 		udp->all_zombies = NULL;
1675 		udp->nzombies = 0;
1676 	}
1677 	trim_stack_cache(0);
1678 
1679 	/*
1680 	 * Do post-fork1 processing for subsystems that need it.
1681 	 */
1682 	postfork1_child_tpool();
1683 	postfork1_child_sigev_aio();
1684 	postfork1_child_sigev_mq();
1685 	postfork1_child_sigev_timer();
1686 	postfork1_child_aio();
1687 }
1688 
1689 lwpid_t
1690 lwp_self(void)
1691 {
1692 	return (curthread->ul_lwpid);
1693 }
1694 
1695 #pragma weak _ti_thr_self = _thr_self
1696 #pragma weak thr_self = _thr_self
1697 #pragma weak pthread_self = _thr_self
1698 #pragma weak _pthread_self = _thr_self
1699 thread_t
1700 _thr_self()
1701 {
1702 	return (curthread->ul_lwpid);
1703 }
1704 
1705 #pragma weak thr_main = _thr_main
1706 int
1707 _thr_main()
1708 {
1709 	ulwp_t *self = __curthread();
1710 
1711 	return ((self == NULL)? -1 : self->ul_main);
1712 }
1713 
1714 int
1715 _thrp_cancelled(void)
1716 {
1717 	return (curthread->ul_rval == PTHREAD_CANCELED);
1718 }
1719 
1720 int
1721 _thrp_stksegment(ulwp_t *ulwp, stack_t *stk)
1722 {
1723 	stk->ss_sp = (void *)ulwp->ul_stktop;
1724 	stk->ss_size = ulwp->ul_stksiz;
1725 	stk->ss_flags = 0;
1726 	return (0);
1727 }
1728 
1729 #pragma weak thr_stksegment = _thr_stksegment
1730 int
1731 _thr_stksegment(stack_t *stk)
1732 {
1733 	return (_thrp_stksegment(curthread, stk));
1734 }
1735 
1736 void
1737 force_continue(ulwp_t *ulwp)
1738 {
1739 #if defined(THREAD_DEBUG)
1740 	ulwp_t *self = curthread;
1741 	uberdata_t *udp = self->ul_uberdata;
1742 #endif
1743 	int error;
1744 	timespec_t ts;
1745 
1746 	ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
1747 	ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self));
1748 
1749 	for (;;) {
1750 		error = __lwp_continue(ulwp->ul_lwpid);
1751 		if (error != 0 && error != EINTR)
1752 			break;
1753 		error = 0;
1754 		if (ulwp->ul_stopping) {	/* he is stopping himself */
1755 			ts.tv_sec = 0;		/* give him a chance to run */
1756 			ts.tv_nsec = 100000;	/* 100 usecs or clock tick */
1757 			(void) __nanosleep(&ts, NULL);
1758 		}
1759 		if (!ulwp->ul_stopping)		/* he is running now */
1760 			break;			/* so we are done */
1761 		/*
1762 		 * He is marked as being in the process of stopping
1763 		 * himself.  Loop around and continue him again.
1764 		 * He may not have been stopped the first time.
1765 		 */
1766 	}
1767 }
1768 
1769 /*
1770  * Suspend an lwp with lwp_suspend(), then move it to a safe
1771  * point, that is, to a point where ul_critical is zero.
1772  * On return, the ulwp_lock() is dropped as with ulwp_unlock().
1773  * If 'link_dropped' is non-NULL, then 'link_lock' is held on entry.
1774  * If we have to drop link_lock, we store 1 through link_dropped.
1775  * If the lwp exits before it can be suspended, we return ESRCH.
1776  */
1777 int
1778 safe_suspend(ulwp_t *ulwp, uchar_t whystopped, int *link_dropped)
1779 {
1780 	ulwp_t *self = curthread;
1781 	uberdata_t *udp = self->ul_uberdata;
1782 	cond_t *cvp = ulwp_condvar(ulwp, udp);
1783 	mutex_t *mp = ulwp_mutex(ulwp, udp);
1784 	thread_t tid = ulwp->ul_lwpid;
1785 	int ix = ulwp->ul_ix;
1786 	int error = 0;
1787 
1788 	ASSERT(whystopped == TSTP_REGULAR ||
1789 	    whystopped == TSTP_MUTATOR ||
1790 	    whystopped == TSTP_FORK);
1791 	ASSERT(ulwp != self);
1792 	ASSERT(!ulwp->ul_stop);
1793 	ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
1794 	ASSERT(MUTEX_OWNED(mp, self));
1795 
1796 	if (link_dropped != NULL)
1797 		*link_dropped = 0;
1798 
1799 	/*
1800 	 * We must grab the target's spin lock before suspending it.
1801 	 * See the comments below and in _thrp_suspend() for why.
1802 	 */
1803 	spin_lock_set(&ulwp->ul_spinlock);
1804 	(void) ___lwp_suspend(tid);
1805 	spin_lock_clear(&ulwp->ul_spinlock);
1806 
1807 top:
1808 	if (ulwp->ul_critical == 0 || ulwp->ul_stopping) {
1809 		/* thread is already safe */
1810 		ulwp->ul_stop |= whystopped;
1811 	} else {
1812 		/*
1813 		 * Setting ul_pleasestop causes the target thread to stop
1814 		 * itself in _thrp_suspend(), below, after we drop its lock.
1815 		 * We must continue the critical thread before dropping
1816 		 * link_lock because the critical thread may be holding
1817 		 * the queue lock for link_lock.  This is delicate.
1818 		 */
1819 		ulwp->ul_pleasestop |= whystopped;
1820 		force_continue(ulwp);
1821 		if (link_dropped != NULL) {
1822 			*link_dropped = 1;
1823 			lmutex_unlock(&udp->link_lock);
1824 			/* be sure to drop link_lock only once */
1825 			link_dropped = NULL;
1826 		}
1827 
1828 		/*
1829 		 * The thread may disappear by calling thr_exit() so we
1830 		 * cannot rely on the ulwp pointer after dropping the lock.
1831 		 * Instead, we search the hash table to find it again.
1832 		 * When we return, we may find that the thread has been
1833 		 * continued by some other thread.  The suspend/continue
1834 		 * interfaces are prone to such race conditions by design.
1835 		 */
1836 		while (ulwp && !ulwp->ul_dead && !ulwp->ul_stop &&
1837 		    (ulwp->ul_pleasestop & whystopped)) {
1838 			(void) __cond_wait(cvp, mp);
1839 			for (ulwp = udp->thr_hash_table[ix].hash_bucket;
1840 			    ulwp != NULL; ulwp = ulwp->ul_hash) {
1841 				if (ulwp->ul_lwpid == tid)
1842 					break;
1843 			}
1844 		}
1845 
1846 		if (ulwp == NULL || ulwp->ul_dead)
1847 			error = ESRCH;
1848 		else {
1849 			/*
1850 			 * Do another lwp_suspend() to make sure we don't
1851 			 * return until the target thread is fully stopped
1852 			 * in the kernel.  Don't apply lwp_suspend() until
1853 			 * we know that the target is not holding any
1854 			 * queue locks, that is, that it has completed
1855 			 * ulwp_unlock(self) and has, or at least is
1856 			 * about to, call lwp_suspend() on itself.  We do
1857 			 * this by grabbing the target's spin lock.
1858 			 */
1859 			ASSERT(ulwp->ul_lwpid == tid);
1860 			spin_lock_set(&ulwp->ul_spinlock);
1861 			(void) ___lwp_suspend(tid);
1862 			spin_lock_clear(&ulwp->ul_spinlock);
1863 			/*
1864 			 * If some other thread did a thr_continue()
1865 			 * on the target thread we have to start over.
1866 			 */
1867 			if (!ulwp->ul_stopping || !(ulwp->ul_stop & whystopped))
1868 				goto top;
1869 		}
1870 	}
1871 
1872 	(void) cond_broadcast_internal(cvp);
1873 	lmutex_unlock(mp);
1874 	return (error);
1875 }
1876 
1877 int
1878 _thrp_suspend(thread_t tid, uchar_t whystopped)
1879 {
1880 	ulwp_t *self = curthread;
1881 	uberdata_t *udp = self->ul_uberdata;
1882 	ulwp_t *ulwp;
1883 	int error = 0;
1884 
1885 	ASSERT((whystopped & (TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) != 0);
1886 	ASSERT((whystopped & ~(TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) == 0);
1887 
1888 	/*
1889 	 * We can't suspend anyone except ourself while
1890 	 * some other thread is performing a fork.
1891 	 * This also allows only one suspension at a time.
1892 	 */
1893 	if (tid != self->ul_lwpid)
1894 		fork_lock_enter();
1895 
1896 	if ((ulwp = find_lwp(tid)) == NULL)
1897 		error = ESRCH;
1898 	else if (whystopped == TSTP_MUTATOR && !ulwp->ul_mutator) {
1899 		ulwp_unlock(ulwp, udp);
1900 		error = EINVAL;
1901 	} else if (ulwp->ul_stop) {	/* already stopped */
1902 		ulwp->ul_stop |= whystopped;
1903 		ulwp_broadcast(ulwp);
1904 		ulwp_unlock(ulwp, udp);
1905 	} else if (ulwp != self) {
1906 		/*
1907 		 * After suspending the other thread, move it out of a
1908 		 * critical section and deal with the schedctl mappings.
1909 		 * safe_suspend() suspends the other thread, calls
1910 		 * ulwp_broadcast(ulwp) and drops the ulwp lock.
1911 		 */
1912 		error = safe_suspend(ulwp, whystopped, NULL);
1913 	} else {
1914 		int schedctl_after_fork = 0;
1915 
1916 		/*
1917 		 * We are suspending ourself.  We must not take a signal
1918 		 * until we return from lwp_suspend() and clear ul_stopping.
1919 		 * This is to guard against siglongjmp().
1920 		 */
1921 		enter_critical(self);
1922 		self->ul_sp = stkptr();
1923 		_flush_windows();	/* sparc */
1924 		self->ul_pleasestop = 0;
1925 		self->ul_stop |= whystopped;
1926 		/*
1927 		 * Grab our spin lock before dropping ulwp_mutex(self).
1928 		 * This prevents the suspending thread from applying
1929 		 * lwp_suspend() to us before we emerge from
1930 		 * lmutex_unlock(mp) and have dropped mp's queue lock.
1931 		 */
1932 		spin_lock_set(&self->ul_spinlock);
1933 		self->ul_stopping = 1;
1934 		ulwp_broadcast(self);
1935 		ulwp_unlock(self, udp);
1936 		/*
1937 		 * From this point until we return from lwp_suspend(),
1938 		 * we must not call any function that might invoke the
1939 		 * dynamic linker, that is, we can only call functions
1940 		 * private to the library.
1941 		 *
1942 		 * Also, this is a nasty race condition for a process
1943 		 * that is undergoing a forkall() operation:
1944 		 * Once we clear our spinlock (below), we are vulnerable
1945 		 * to being suspended by the forkall() thread before
1946 		 * we manage to suspend ourself in ___lwp_suspend().
1947 		 * See safe_suspend() and force_continue().
1948 		 *
1949 		 * To avoid a SIGSEGV due to the disappearance
1950 		 * of the schedctl mappings in the child process,
1951 		 * which can happen in spin_lock_clear() if we
1952 		 * are suspended while we are in the middle of
1953 		 * its call to preempt(), we preemptively clear
1954 		 * our own schedctl pointer before dropping our
1955 		 * spinlock.  We reinstate it, in both the parent
1956 		 * and (if this really is a forkall()) the child.
1957 		 */
1958 		if (whystopped & TSTP_FORK) {
1959 			schedctl_after_fork = 1;
1960 			self->ul_schedctl = NULL;
1961 			self->ul_schedctl_called = &udp->uberflags;
1962 		}
1963 		spin_lock_clear(&self->ul_spinlock);
1964 		(void) ___lwp_suspend(tid);
1965 		/*
1966 		 * Somebody else continued us.
1967 		 * We can't grab ulwp_lock(self)
1968 		 * until after clearing ul_stopping.
1969 		 * force_continue() relies on this.
1970 		 */
1971 		self->ul_stopping = 0;
1972 		self->ul_sp = 0;
1973 		if (schedctl_after_fork) {
1974 			self->ul_schedctl_called = NULL;
1975 			self->ul_schedctl = NULL;
1976 			(void) setup_schedctl();
1977 		}
1978 		ulwp_lock(self, udp);
1979 		ulwp_broadcast(self);
1980 		ulwp_unlock(self, udp);
1981 		exit_critical(self);
1982 	}
1983 
1984 	if (tid != self->ul_lwpid)
1985 		fork_lock_exit();
1986 
1987 	return (error);
1988 }
1989 
1990 /*
1991  * Suspend all lwps other than ourself in preparation for fork.
1992  */
1993 void
1994 suspend_fork()
1995 {
1996 	ulwp_t *self = curthread;
1997 	uberdata_t *udp = self->ul_uberdata;
1998 	ulwp_t *ulwp;
1999 	int link_dropped;
2000 
2001 	ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
2002 top:
2003 	lmutex_lock(&udp->link_lock);
2004 
2005 	for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2006 		ulwp_lock(ulwp, udp);
2007 		if (ulwp->ul_stop) {	/* already stopped */
2008 			ulwp->ul_stop |= TSTP_FORK;
2009 			ulwp_broadcast(ulwp);
2010 			ulwp_unlock(ulwp, udp);
2011 		} else {
2012 			/*
2013 			 * Move the stopped lwp out of a critical section.
2014 			 */
2015 			if (safe_suspend(ulwp, TSTP_FORK, &link_dropped) ||
2016 			    link_dropped)
2017 				goto top;
2018 		}
2019 	}
2020 
2021 	lmutex_unlock(&udp->link_lock);
2022 }
2023 
2024 void
2025 continue_fork(int child)
2026 {
2027 	ulwp_t *self = curthread;
2028 	uberdata_t *udp = self->ul_uberdata;
2029 	ulwp_t *ulwp;
2030 
2031 	ASSERT(MUTEX_OWNED(&udp->fork_lock, self));
2032 
2033 	/*
2034 	 * Clear the schedctl pointers in the child of forkall().
2035 	 */
2036 	if (child) {
2037 		for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2038 			ulwp->ul_schedctl_called =
2039 			    ulwp->ul_dead? &udp->uberflags : NULL;
2040 			ulwp->ul_schedctl = NULL;
2041 		}
2042 	}
2043 
2044 	/*
2045 	 * Set all lwps that were stopped for fork() running again.
2046 	 */
2047 	lmutex_lock(&udp->link_lock);
2048 	for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2049 		mutex_t *mp = ulwp_mutex(ulwp, udp);
2050 		lmutex_lock(mp);
2051 		ASSERT(ulwp->ul_stop & TSTP_FORK);
2052 		ulwp->ul_stop &= ~TSTP_FORK;
2053 		ulwp_broadcast(ulwp);
2054 		if (!ulwp->ul_stop)
2055 			force_continue(ulwp);
2056 		lmutex_unlock(mp);
2057 	}
2058 	lmutex_unlock(&udp->link_lock);
2059 }
2060 
2061 int
2062 _thrp_continue(thread_t tid, uchar_t whystopped)
2063 {
2064 	uberdata_t *udp = curthread->ul_uberdata;
2065 	ulwp_t *ulwp;
2066 	mutex_t *mp;
2067 	int error = 0;
2068 
2069 	ASSERT(whystopped == TSTP_REGULAR ||
2070 	    whystopped == TSTP_MUTATOR);
2071 
2072 	/*
2073 	 * We single-thread the entire thread suspend/continue mechanism.
2074 	 */
2075 	fork_lock_enter();
2076 
2077 	if ((ulwp = find_lwp(tid)) == NULL) {
2078 		fork_lock_exit();
2079 		return (ESRCH);
2080 	}
2081 
2082 	mp = ulwp_mutex(ulwp, udp);
2083 	if ((whystopped == TSTP_MUTATOR && !ulwp->ul_mutator)) {
2084 		error = EINVAL;
2085 	} else if (ulwp->ul_stop & whystopped) {
2086 		ulwp->ul_stop &= ~whystopped;
2087 		ulwp_broadcast(ulwp);
2088 		if (!ulwp->ul_stop) {
2089 			if (whystopped == TSTP_REGULAR && ulwp->ul_created) {
2090 				ulwp->ul_sp = 0;
2091 				ulwp->ul_created = 0;
2092 			}
2093 			force_continue(ulwp);
2094 		}
2095 	}
2096 	lmutex_unlock(mp);
2097 
2098 	fork_lock_exit();
2099 	return (error);
2100 }
2101 
2102 #pragma weak thr_suspend = _thr_suspend
2103 int
2104 _thr_suspend(thread_t tid)
2105 {
2106 	return (_thrp_suspend(tid, TSTP_REGULAR));
2107 }
2108 
2109 #pragma weak thr_continue = _thr_continue
2110 int
2111 _thr_continue(thread_t tid)
2112 {
2113 	return (_thrp_continue(tid, TSTP_REGULAR));
2114 }
2115 
2116 #pragma weak thr_yield = _thr_yield
2117 void
2118 _thr_yield()
2119 {
2120 	yield();
2121 }
2122 
2123 #pragma weak thr_kill = _thr_kill
2124 #pragma weak pthread_kill = _thr_kill
2125 #pragma weak _pthread_kill = _thr_kill
2126 int
2127 _thr_kill(thread_t tid, int sig)
2128 {
2129 	if (sig == SIGCANCEL)
2130 		return (EINVAL);
2131 	return (__lwp_kill(tid, sig));
2132 }
2133 
2134 /*
2135  * Exit a critical section, take deferred actions if necessary.
2136  */
2137 void
2138 do_exit_critical()
2139 {
2140 	ulwp_t *self = curthread;
2141 	int sig;
2142 
2143 	ASSERT(self->ul_critical == 0);
2144 	if (self->ul_dead)
2145 		return;
2146 
2147 	while (self->ul_pleasestop ||
2148 	    (self->ul_cursig != 0 && self->ul_sigdefer == 0)) {
2149 		/*
2150 		 * Avoid a recursive call to exit_critical() in _thrp_suspend()
2151 		 * by keeping self->ul_critical == 1 here.
2152 		 */
2153 		self->ul_critical++;
2154 		while (self->ul_pleasestop) {
2155 			/*
2156 			 * Guard against suspending ourself while on a sleep
2157 			 * queue.  See the comments in call_user_handler().
2158 			 */
2159 			unsleep_self();
2160 			set_parking_flag(self, 0);
2161 			(void) _thrp_suspend(self->ul_lwpid,
2162 			    self->ul_pleasestop);
2163 		}
2164 		self->ul_critical--;
2165 
2166 		if ((sig = self->ul_cursig) != 0 && self->ul_sigdefer == 0) {
2167 			/*
2168 			 * Clear ul_cursig before proceeding.
2169 			 * This protects us from the dynamic linker's
2170 			 * calls to bind_guard()/bind_clear() in the
2171 			 * event that it is invoked to resolve a symbol
2172 			 * like take_deferred_signal() below.
2173 			 */
2174 			self->ul_cursig = 0;
2175 			take_deferred_signal(sig);
2176 			ASSERT(self->ul_cursig == 0);
2177 		}
2178 	}
2179 	ASSERT(self->ul_critical == 0);
2180 }
2181 
2182 /*
2183  * _ti_bind_guard() and _ti_bind_clear() are called by the dynamic linker
2184  * (ld.so.1) when it has do do something, like resolve a symbol to be called
2185  * by the application or one of its libraries.  _ti_bind_guard() is called
2186  * on entry to ld.so.1, _ti_bind_clear() on exit from ld.so.1 back to the
2187  * application.  The dynamic linker gets special dispensation from libc to
2188  * run in a critical region (all signals deferred and no thread suspension
2189  * or forking allowed), and to be immune from cancellation for the duration.
2190  */
2191 int
2192 _ti_bind_guard(int flags)
2193 {
2194 	ulwp_t *self = curthread;
2195 	uberdata_t *udp = self->ul_uberdata;
2196 	int bindflag = (flags & THR_FLG_RTLD);
2197 
2198 	if ((self->ul_bindflags & bindflag) == bindflag)
2199 		return (0);
2200 	if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
2201 		ASSERT(self->ul_critical == 0);
2202 		sigoff(self);	/* see no signals while holding ld_lock */
2203 		mutex_lock(&udp->ld_lock);
2204 	}
2205 	enter_critical(self);
2206 	self->ul_save_state = self->ul_cancel_disabled;
2207 	self->ul_cancel_disabled = 1;
2208 	set_cancel_pending_flag(self, 0);
2209 	self->ul_bindflags |= bindflag;
2210 	return (1);
2211 }
2212 
2213 int
2214 _ti_bind_clear(int flags)
2215 {
2216 	ulwp_t *self = curthread;
2217 	uberdata_t *udp = self->ul_uberdata;
2218 	int bindflag = (flags & THR_FLG_RTLD);
2219 
2220 	if ((self->ul_bindflags & bindflag) == 0)
2221 		return (self->ul_bindflags);
2222 	self->ul_bindflags &= ~bindflag;
2223 	self->ul_cancel_disabled = self->ul_save_state;
2224 	set_cancel_pending_flag(self, 0);
2225 	exit_critical(self);
2226 	if ((flags & (THR_FLG_NOLOCK | THR_FLG_REENTER)) == THR_FLG_NOLOCK) {
2227 		ASSERT(self->ul_critical == 0);
2228 		if (MUTEX_OWNED(&udp->ld_lock, self)) {
2229 			mutex_unlock(&udp->ld_lock);
2230 			sigon(self);	/* reenable signals */
2231 		}
2232 	}
2233 	return (self->ul_bindflags);
2234 }
2235 
2236 /*
2237  * sigoff() and sigon() enable cond_wait() to behave (optionally) like
2238  * it does in the old libthread (see the comments in cond_wait_queue()).
2239  * Also, signals are deferred at thread startup until TLS constructors
2240  * have all been called, at which time _thr_setup() calls sigon().
2241  *
2242  * _sigoff() and _sigon() are external consolidation-private interfaces to
2243  * sigoff() and sigon(), respectively, in libc.  These are used in libnsl.
2244  * Also, _sigoff() and _sigon() are called from dbx's run-time checking
2245  * (librtc.so) to defer signals during its critical sections (not to be
2246  * confused with libc critical sections [see exit_critical() above]).
2247  */
2248 void
2249 _sigoff(void)
2250 {
2251 	sigoff(curthread);
2252 }
2253 
2254 void
2255 _sigon(void)
2256 {
2257 	sigon(curthread);
2258 }
2259 
2260 void
2261 sigon(ulwp_t *self)
2262 {
2263 	int sig;
2264 
2265 	ASSERT(self->ul_sigdefer > 0);
2266 	if (--self->ul_sigdefer == 0) {
2267 		if ((sig = self->ul_cursig) != 0 && self->ul_critical == 0) {
2268 			self->ul_cursig = 0;
2269 			take_deferred_signal(sig);
2270 			ASSERT(self->ul_cursig == 0);
2271 		}
2272 	}
2273 }
2274 
2275 #pragma weak thr_getconcurrency = _thr_getconcurrency
2276 int
2277 _thr_getconcurrency()
2278 {
2279 	return (thr_concurrency);
2280 }
2281 
2282 #pragma weak pthread_getconcurrency = _pthread_getconcurrency
2283 int
2284 _pthread_getconcurrency()
2285 {
2286 	return (pthread_concurrency);
2287 }
2288 
2289 #pragma weak thr_setconcurrency = _thr_setconcurrency
2290 int
2291 _thr_setconcurrency(int new_level)
2292 {
2293 	uberdata_t *udp = curthread->ul_uberdata;
2294 
2295 	if (new_level < 0)
2296 		return (EINVAL);
2297 	if (new_level > 65536)		/* 65536 is totally arbitrary */
2298 		return (EAGAIN);
2299 	lmutex_lock(&udp->link_lock);
2300 	if (new_level > thr_concurrency)
2301 		thr_concurrency = new_level;
2302 	lmutex_unlock(&udp->link_lock);
2303 	return (0);
2304 }
2305 
2306 #pragma weak pthread_setconcurrency = _pthread_setconcurrency
2307 int
2308 _pthread_setconcurrency(int new_level)
2309 {
2310 	if (new_level < 0)
2311 		return (EINVAL);
2312 	if (new_level > 65536)		/* 65536 is totally arbitrary */
2313 		return (EAGAIN);
2314 	pthread_concurrency = new_level;
2315 	return (0);
2316 }
2317 
2318 #pragma weak thr_min_stack = _thr_min_stack
2319 #pragma weak __pthread_min_stack = _thr_min_stack
2320 size_t
2321 _thr_min_stack(void)
2322 {
2323 	return (MINSTACK);
2324 }
2325 
2326 int
2327 __nthreads(void)
2328 {
2329 	return (curthread->ul_uberdata->nthreads);
2330 }
2331 
2332 /*
2333  * XXX
2334  * The remainder of this file implements the private interfaces to java for
2335  * garbage collection.  It is no longer used, at least by java 1.2.
2336  * It can all go away once all old JVMs have disappeared.
2337  */
2338 
2339 int	suspendingallmutators;	/* when non-zero, suspending all mutators. */
2340 int	suspendedallmutators;	/* when non-zero, all mutators suspended. */
2341 int	mutatorsbarrier;	/* when non-zero, mutators barrier imposed. */
2342 mutex_t	mutatorslock = DEFAULTMUTEX;	/* used to enforce mutators barrier. */
2343 cond_t	mutatorscv = DEFAULTCV;		/* where non-mutators sleep. */
2344 
2345 /*
2346  * Get the available register state for the target thread.
2347  * Return non-volatile registers: TRS_NONVOLATILE
2348  */
2349 #pragma weak thr_getstate = _thr_getstate
2350 int
2351 _thr_getstate(thread_t tid, int *flag, lwpid_t *lwp, stack_t *ss, gregset_t rs)
2352 {
2353 	ulwp_t *self = curthread;
2354 	uberdata_t *udp = self->ul_uberdata;
2355 	ulwp_t **ulwpp;
2356 	ulwp_t *ulwp;
2357 	int error = 0;
2358 	int trs_flag = TRS_LWPID;
2359 
2360 	if (tid == 0 || self->ul_lwpid == tid) {
2361 		ulwp = self;
2362 		ulwp_lock(ulwp, udp);
2363 	} else if ((ulwpp = find_lwpp(tid)) != NULL) {
2364 		ulwp = *ulwpp;
2365 	} else {
2366 		if (flag)
2367 			*flag = TRS_INVALID;
2368 		return (ESRCH);
2369 	}
2370 
2371 	if (ulwp->ul_dead) {
2372 		trs_flag = TRS_INVALID;
2373 	} else if (!ulwp->ul_stop && !suspendedallmutators) {
2374 		error = EINVAL;
2375 		trs_flag = TRS_INVALID;
2376 	} else if (ulwp->ul_stop) {
2377 		trs_flag = TRS_NONVOLATILE;
2378 		getgregs(ulwp, rs);
2379 	}
2380 
2381 	if (flag)
2382 		*flag = trs_flag;
2383 	if (lwp)
2384 		*lwp = tid;
2385 	if (ss != NULL)
2386 		(void) _thrp_stksegment(ulwp, ss);
2387 
2388 	ulwp_unlock(ulwp, udp);
2389 	return (error);
2390 }
2391 
2392 /*
2393  * Set the appropriate register state for the target thread.
2394  * This is not used by java.  It exists solely for the MSTC test suite.
2395  */
2396 #pragma weak thr_setstate = _thr_setstate
2397 int
2398 _thr_setstate(thread_t tid, int flag, gregset_t rs)
2399 {
2400 	uberdata_t *udp = curthread->ul_uberdata;
2401 	ulwp_t *ulwp;
2402 	int error = 0;
2403 
2404 	if ((ulwp = find_lwp(tid)) == NULL)
2405 		return (ESRCH);
2406 
2407 	if (!ulwp->ul_stop && !suspendedallmutators)
2408 		error = EINVAL;
2409 	else if (rs != NULL) {
2410 		switch (flag) {
2411 		case TRS_NONVOLATILE:
2412 			/* do /proc stuff here? */
2413 			if (ulwp->ul_stop)
2414 				setgregs(ulwp, rs);
2415 			else
2416 				error = EINVAL;
2417 			break;
2418 		case TRS_LWPID:		/* do /proc stuff here? */
2419 		default:
2420 			error = EINVAL;
2421 			break;
2422 		}
2423 	}
2424 
2425 	ulwp_unlock(ulwp, udp);
2426 	return (error);
2427 }
2428 
2429 int
2430 getlwpstatus(thread_t tid, struct lwpstatus *sp)
2431 {
2432 	extern ssize_t __pread(int, void *, size_t, off_t);
2433 	char buf[100];
2434 	int fd;
2435 
2436 	/* "/proc/self/lwp/%u/lwpstatus" w/o stdio */
2437 	(void) strcpy(buf, "/proc/self/lwp/");
2438 	ultos((uint64_t)tid, 10, buf + strlen(buf));
2439 	(void) strcat(buf, "/lwpstatus");
2440 	if ((fd = __open(buf, O_RDONLY, 0)) >= 0) {
2441 		while (__pread(fd, sp, sizeof (*sp), 0) == sizeof (*sp)) {
2442 			if (sp->pr_flags & PR_STOPPED) {
2443 				(void) __close(fd);
2444 				return (0);
2445 			}
2446 			yield();	/* give him a chance to stop */
2447 		}
2448 		(void) __close(fd);
2449 	}
2450 	return (-1);
2451 }
2452 
2453 int
2454 putlwpregs(thread_t tid, prgregset_t prp)
2455 {
2456 	extern ssize_t __writev(int, const struct iovec *, int);
2457 	char buf[100];
2458 	int fd;
2459 	long dstop_sreg[2];
2460 	long run_null[2];
2461 	iovec_t iov[3];
2462 
2463 	/* "/proc/self/lwp/%u/lwpctl" w/o stdio */
2464 	(void) strcpy(buf, "/proc/self/lwp/");
2465 	ultos((uint64_t)tid, 10, buf + strlen(buf));
2466 	(void) strcat(buf, "/lwpctl");
2467 	if ((fd = __open(buf, O_WRONLY, 0)) >= 0) {
2468 		dstop_sreg[0] = PCDSTOP;	/* direct it to stop */
2469 		dstop_sreg[1] = PCSREG;		/* set the registers */
2470 		iov[0].iov_base = (caddr_t)dstop_sreg;
2471 		iov[0].iov_len = sizeof (dstop_sreg);
2472 		iov[1].iov_base = (caddr_t)prp;	/* from the register set */
2473 		iov[1].iov_len = sizeof (prgregset_t);
2474 		run_null[0] = PCRUN;		/* make it runnable again */
2475 		run_null[1] = 0;
2476 		iov[2].iov_base = (caddr_t)run_null;
2477 		iov[2].iov_len = sizeof (run_null);
2478 		if (__writev(fd, iov, 3) >= 0) {
2479 			(void) __close(fd);
2480 			return (0);
2481 		}
2482 		(void) __close(fd);
2483 	}
2484 	return (-1);
2485 }
2486 
2487 static ulong_t
2488 gettsp_slow(thread_t tid)
2489 {
2490 	char buf[100];
2491 	struct lwpstatus status;
2492 
2493 	if (getlwpstatus(tid, &status) != 0) {
2494 		/* "__gettsp(%u): can't read lwpstatus" w/o stdio */
2495 		(void) strcpy(buf, "__gettsp(");
2496 		ultos((uint64_t)tid, 10, buf + strlen(buf));
2497 		(void) strcat(buf, "): can't read lwpstatus");
2498 		thr_panic(buf);
2499 	}
2500 	return (status.pr_reg[R_SP]);
2501 }
2502 
2503 ulong_t
2504 __gettsp(thread_t tid)
2505 {
2506 	uberdata_t *udp = curthread->ul_uberdata;
2507 	ulwp_t *ulwp;
2508 	ulong_t result;
2509 
2510 	if ((ulwp = find_lwp(tid)) == NULL)
2511 		return (0);
2512 
2513 	if (ulwp->ul_stop && (result = ulwp->ul_sp) != 0) {
2514 		ulwp_unlock(ulwp, udp);
2515 		return (result);
2516 	}
2517 
2518 	result = gettsp_slow(tid);
2519 	ulwp_unlock(ulwp, udp);
2520 	return (result);
2521 }
2522 
2523 /*
2524  * This tells java stack walkers how to find the ucontext
2525  * structure passed to signal handlers.
2526  */
2527 #pragma weak thr_sighndlrinfo = _thr_sighndlrinfo
2528 void
2529 _thr_sighndlrinfo(void (**func)(), int *funcsize)
2530 {
2531 	*func = &__sighndlr;
2532 	*funcsize = (char *)&__sighndlrend - (char *)&__sighndlr;
2533 }
2534 
2535 /*
2536  * Mark a thread a mutator or reset a mutator to being a default,
2537  * non-mutator thread.
2538  */
2539 #pragma weak thr_setmutator = _thr_setmutator
2540 int
2541 _thr_setmutator(thread_t tid, int enabled)
2542 {
2543 	ulwp_t *self = curthread;
2544 	uberdata_t *udp = self->ul_uberdata;
2545 	ulwp_t *ulwp;
2546 	int error;
2547 	int cancel_state;
2548 
2549 	enabled = enabled? 1 : 0;
2550 top:
2551 	if (tid == 0) {
2552 		ulwp = self;
2553 		ulwp_lock(ulwp, udp);
2554 	} else if ((ulwp = find_lwp(tid)) == NULL) {
2555 		return (ESRCH);
2556 	}
2557 
2558 	/*
2559 	 * The target thread should be the caller itself or a suspended thread.
2560 	 * This prevents the target from also changing its ul_mutator field.
2561 	 */
2562 	error = 0;
2563 	if (ulwp != self && !ulwp->ul_stop && enabled)
2564 		error = EINVAL;
2565 	else if (ulwp->ul_mutator != enabled) {
2566 		lmutex_lock(&mutatorslock);
2567 		if (mutatorsbarrier) {
2568 			ulwp_unlock(ulwp, udp);
2569 			(void) _pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
2570 			    &cancel_state);
2571 			while (mutatorsbarrier)
2572 				(void) _cond_wait(&mutatorscv, &mutatorslock);
2573 			(void) _pthread_setcancelstate(cancel_state, NULL);
2574 			lmutex_unlock(&mutatorslock);
2575 			goto top;
2576 		}
2577 		ulwp->ul_mutator = enabled;
2578 		lmutex_unlock(&mutatorslock);
2579 	}
2580 
2581 	ulwp_unlock(ulwp, udp);
2582 	return (error);
2583 }
2584 
2585 /*
2586  * Establish a barrier against new mutators.  Any non-mutator trying
2587  * to become a mutator is suspended until the barrier is removed.
2588  */
2589 #pragma weak thr_mutators_barrier = _thr_mutators_barrier
2590 void
2591 _thr_mutators_barrier(int enabled)
2592 {
2593 	int oldvalue;
2594 	int cancel_state;
2595 
2596 	lmutex_lock(&mutatorslock);
2597 
2598 	/*
2599 	 * Wait if trying to set the barrier while it is already set.
2600 	 */
2601 	(void) _pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
2602 	while (mutatorsbarrier && enabled)
2603 		(void) _cond_wait(&mutatorscv, &mutatorslock);
2604 	(void) _pthread_setcancelstate(cancel_state, NULL);
2605 
2606 	oldvalue = mutatorsbarrier;
2607 	mutatorsbarrier = enabled;
2608 	/*
2609 	 * Wakeup any blocked non-mutators when barrier is removed.
2610 	 */
2611 	if (oldvalue && !enabled)
2612 		(void) cond_broadcast_internal(&mutatorscv);
2613 	lmutex_unlock(&mutatorslock);
2614 }
2615 
2616 /*
2617  * Suspend the set of all mutators except for the caller.  The list
2618  * of actively running threads is searched and only the mutators
2619  * in this list are suspended.  Actively running non-mutators remain
2620  * running.  Any other thread is suspended.
2621  */
2622 #pragma weak thr_suspend_allmutators = _thr_suspend_allmutators
2623 int
2624 _thr_suspend_allmutators(void)
2625 {
2626 	ulwp_t *self = curthread;
2627 	uberdata_t *udp = self->ul_uberdata;
2628 	ulwp_t *ulwp;
2629 	int link_dropped;
2630 
2631 	/*
2632 	 * We single-thread the entire thread suspend/continue mechanism.
2633 	 */
2634 	fork_lock_enter();
2635 
2636 top:
2637 	lmutex_lock(&udp->link_lock);
2638 
2639 	if (suspendingallmutators || suspendedallmutators) {
2640 		lmutex_unlock(&udp->link_lock);
2641 		fork_lock_exit();
2642 		return (EINVAL);
2643 	}
2644 	suspendingallmutators = 1;
2645 
2646 	for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2647 		ulwp_lock(ulwp, udp);
2648 		if (!ulwp->ul_mutator) {
2649 			ulwp_unlock(ulwp, udp);
2650 		} else if (ulwp->ul_stop) {	/* already stopped */
2651 			ulwp->ul_stop |= TSTP_MUTATOR;
2652 			ulwp_broadcast(ulwp);
2653 			ulwp_unlock(ulwp, udp);
2654 		} else {
2655 			/*
2656 			 * Move the stopped lwp out of a critical section.
2657 			 */
2658 			if (safe_suspend(ulwp, TSTP_MUTATOR, &link_dropped) ||
2659 			    link_dropped) {
2660 				suspendingallmutators = 0;
2661 				goto top;
2662 			}
2663 		}
2664 	}
2665 
2666 	suspendedallmutators = 1;
2667 	suspendingallmutators = 0;
2668 	lmutex_unlock(&udp->link_lock);
2669 	fork_lock_exit();
2670 	return (0);
2671 }
2672 
2673 /*
2674  * Suspend the target mutator.  The caller is permitted to suspend
2675  * itself.  If a mutator barrier is enabled, the caller will suspend
2676  * itself as though it had been suspended by thr_suspend_allmutators().
2677  * When the barrier is removed, this thread will be resumed.  Any
2678  * suspended mutator, whether suspended by thr_suspend_mutator(), or by
2679  * thr_suspend_allmutators(), can be resumed by thr_continue_mutator().
2680  */
2681 #pragma weak thr_suspend_mutator = _thr_suspend_mutator
2682 int
2683 _thr_suspend_mutator(thread_t tid)
2684 {
2685 	if (tid == 0)
2686 		tid = curthread->ul_lwpid;
2687 	return (_thrp_suspend(tid, TSTP_MUTATOR));
2688 }
2689 
2690 /*
2691  * Resume the set of all suspended mutators.
2692  */
2693 #pragma weak thr_continue_allmutators = _thr_continue_allmutators
2694 int
2695 _thr_continue_allmutators()
2696 {
2697 	ulwp_t *self = curthread;
2698 	uberdata_t *udp = self->ul_uberdata;
2699 	ulwp_t *ulwp;
2700 
2701 	/*
2702 	 * We single-thread the entire thread suspend/continue mechanism.
2703 	 */
2704 	fork_lock_enter();
2705 
2706 	lmutex_lock(&udp->link_lock);
2707 	if (!suspendedallmutators) {
2708 		lmutex_unlock(&udp->link_lock);
2709 		fork_lock_exit();
2710 		return (EINVAL);
2711 	}
2712 	suspendedallmutators = 0;
2713 
2714 	for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) {
2715 		mutex_t *mp = ulwp_mutex(ulwp, udp);
2716 		lmutex_lock(mp);
2717 		if (ulwp->ul_stop & TSTP_MUTATOR) {
2718 			ulwp->ul_stop &= ~TSTP_MUTATOR;
2719 			ulwp_broadcast(ulwp);
2720 			if (!ulwp->ul_stop)
2721 				force_continue(ulwp);
2722 		}
2723 		lmutex_unlock(mp);
2724 	}
2725 
2726 	lmutex_unlock(&udp->link_lock);
2727 	fork_lock_exit();
2728 	return (0);
2729 }
2730 
2731 /*
2732  * Resume a suspended mutator.
2733  */
2734 #pragma weak thr_continue_mutator = _thr_continue_mutator
2735 int
2736 _thr_continue_mutator(thread_t tid)
2737 {
2738 	return (_thrp_continue(tid, TSTP_MUTATOR));
2739 }
2740 
2741 #pragma weak thr_wait_mutator = _thr_wait_mutator
2742 int
2743 _thr_wait_mutator(thread_t tid, int dontwait)
2744 {
2745 	uberdata_t *udp = curthread->ul_uberdata;
2746 	ulwp_t *ulwp;
2747 	int cancel_state;
2748 	int error = 0;
2749 
2750 	(void) _pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
2751 top:
2752 	if ((ulwp = find_lwp(tid)) == NULL) {
2753 		(void) _pthread_setcancelstate(cancel_state, NULL);
2754 		return (ESRCH);
2755 	}
2756 
2757 	if (!ulwp->ul_mutator)
2758 		error = EINVAL;
2759 	else if (dontwait) {
2760 		if (!(ulwp->ul_stop & TSTP_MUTATOR))
2761 			error = EWOULDBLOCK;
2762 	} else if (!(ulwp->ul_stop & TSTP_MUTATOR)) {
2763 		cond_t *cvp = ulwp_condvar(ulwp, udp);
2764 		mutex_t *mp = ulwp_mutex(ulwp, udp);
2765 
2766 		(void) _cond_wait(cvp, mp);
2767 		(void) lmutex_unlock(mp);
2768 		goto top;
2769 	}
2770 
2771 	ulwp_unlock(ulwp, udp);
2772 	(void) _pthread_setcancelstate(cancel_state, NULL);
2773 	return (error);
2774 }
2775 
2776 /* PROBE_SUPPORT begin */
2777 
2778 void
2779 thr_probe_setup(void *data)
2780 {
2781 	curthread->ul_tpdp = data;
2782 }
2783 
2784 static void *
2785 _thread_probe_getfunc()
2786 {
2787 	return (curthread->ul_tpdp);
2788 }
2789 
2790 void * (*thr_probe_getfunc_addr)(void) = _thread_probe_getfunc;
2791 
2792 /* ARGSUSED */
2793 void
2794 _resume(ulwp_t *ulwp, caddr_t sp, int dontsave)
2795 {
2796 	/* never called */
2797 }
2798 
2799 /* ARGSUSED */
2800 void
2801 _resume_ret(ulwp_t *oldlwp)
2802 {
2803 	/* never called */
2804 }
2805 
2806 /* PROBE_SUPPORT end */
2807