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