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