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