xref: /titanic_50/usr/src/lib/libc/inc/thr_uberdata.h (revision 43b9c05035ac59f7f7a8e7827598db5a15f30ed3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _THR_UBERDATA_H
28 #define	_THR_UBERDATA_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include <signal.h>
38 #include <ucontext.h>
39 #include <thread.h>
40 #include <pthread.h>
41 #include <link.h>
42 #include <sys/resource.h>
43 #include <sys/lwp.h>
44 #include <errno.h>
45 #include <sys/asm_linkage.h>
46 #include <sys/regset.h>
47 #include <sys/fcntl.h>
48 #include <sys/mman.h>
49 #include <synch.h>
50 #include <door.h>
51 #include <limits.h>
52 #include <sys/synch32.h>
53 #include <schedctl.h>
54 #include <sys/priocntl.h>
55 #include <thread_db.h>
56 #include <setjmp.h>
57 #include "libc_int.h"
58 #include "tdb_agent.h"
59 #include "thr_debug.h"
60 
61 /*
62  * This is an implementation-specific include file for threading support.
63  * It is not to be seen by the clients of the library.
64  *
65  * This file also describes uberdata in libc.
66  *
67  * The term "uberdata" refers to data that is unique and visible across
68  * all link maps.  The name is meant to imply that such data is truly
69  * global, not just locally global to a particular link map.
70  *
71  * See the Linker and Libraries Guide for a full description of alternate
72  * link maps and how they are set up and used.
73  *
74  * Alternate link maps implement multiple global namespaces within a single
75  * process.  There may be multiple instances of identical dynamic libraries
76  * loaded in a process's address space at the same time, each on a different
77  * link map (as determined by the dynamic linker), each with its own set of
78  * global variables.  Which particular instance of a global variable is seen
79  * by a thread running in the process is determined by the link map on which
80  * the thread happens to be executing at the time.
81  *
82  * However, there are aspects of a process that are unique across all
83  * link maps, in particular the structures used to implement threads
84  * of control (in Sparc terminology, there is only one %g7 regardless
85  * of the link map on which the thread is executing).
86  *
87  * All uberdata is referenced from a base pointer in the thread's ulwp_t
88  * structure (which is also uberdata).  All allocations and deallocations
89  * of uberdata are made via the uberdata-aware lmalloc() and lfree()
90  * interfaces (malloc() and free() are simply locally-global).
91  */
92 
93 /*
94  * Special libc-private access to errno.
95  * We do this so that references to errno do not invoke the dynamic linker.
96  */
97 #undef errno
98 #define	errno (*curthread->ul_errnop)
99 
100 /*
101  * See <sys/synch32.h> for the reasons for these values
102  * and why they are different for sparc and intel.
103  */
104 #if defined(__sparc)
105 
106 /* lock.lock64.pad[x]	   4 5 6 7 */
107 #define	LOCKMASK	0xff000000
108 #define	WAITERMASK	0x000000ff
109 #define	SPINNERMASK	0x00ff0000
110 #define	SPINNERSHIFT	16
111 #define	WAITER		0x00000001
112 #define	LOCKSET		0xff
113 #define	LOCKCLEAR	0
114 
115 #define	PIDSHIFT	32
116 #define	LOCKMASK64	0xffffffffff000000ULL
117 #define	LOCKBYTE64	0x00000000ff000000ULL
118 #define	WAITERMASK64	0x00000000000000ffULL
119 #define	SPINNERMASK64	0x0000000000ff0000ULL
120 
121 #elif defined(__x86)
122 
123 /* lock.lock64.pad[x]	   7 6 5 4 */
124 #define	LOCKMASK	0xff000000
125 #define	WAITERMASK	0x00ff0000
126 #define	SPINNERMASK	0x0000ff00
127 #define	SPINNERSHIFT	8
128 #define	WAITER		0x00010000
129 #define	LOCKSET		0x01
130 #define	LOCKCLEAR	0
131 
132 #define	PIDSHIFT	0
133 #define	LOCKMASK64	0xff000000ffffffffULL
134 #define	LOCKBYTE64	0x0100000000000000ULL
135 #define	WAITERMASK64	0x00ff000000000000ULL
136 #define	SPINNERMASK64	0x0000ff0000000000ULL
137 
138 #else
139 #error "neither __sparc nor __x86 is defined"
140 #endif
141 
142 /*
143  * Fetch the owner of a USYNC_THREAD mutex.
144  * Don't use this with process-shared mutexes;
145  * the owing thread may be in a different process.
146  */
147 #define	MUTEX_OWNER(mp)	((ulwp_t *)(uintptr_t)(mp)->mutex_owner)
148 
149 /*
150  * Test if a thread owns a process-private (USYNC_THREAD) mutex.
151  * This is inappropriate for a process-shared (USYNC_PROCESS) mutex.
152  * The 'mp' argument must not have side-effects since it is evaluated twice.
153  */
154 #define	MUTEX_OWNED(mp, thrp)	\
155 	((mp)->mutex_lockw != 0 && MUTEX_OWNER(mp) == thrp)
156 
157 
158 /*
159  * uberflags.uf_tdb_register_sync is an interface with libc_db to enable the
160  * collection of lock statistics by a debugger or other collecting tool.
161  *
162  * uberflags.uf_thread_error_detection is set by an environment variable:
163  *	_THREAD_ERROR_DETECTION
164  *		0 == no detection of locking primitive errors.
165  *		1 == detect errors and issue a warning message.
166  *		2 == detect errors, issue a warning message, and dump core.
167  *
168  * We bundle these together in uberflags.uf_trs_ted to make a test of either
169  * being non-zero a single memory reference (for speed of mutex_lock(), etc).
170  *
171  * uberflags.uf_mt is set non-zero when the first thread (in addition
172  * to the main thread) is created.
173  *
174  * We bundle all these flags together in uberflags.uf_all to make a test
175  * of any being non-zero a single memory reference (again, for speed).
176  */
177 typedef union {
178 	int	uf_all;			/* combined all flags */
179 	struct {
180 		short	h_pad;
181 		short	h_trs_ted;	/* combined reg sync & error detect */
182 	} uf_h;
183 	struct {
184 		char	x_mt;
185 		char	x_pad;
186 		char	x_tdb_register_sync;
187 		char	x_thread_error_detection;
188 	} uf_x;
189 } uberflags_t;
190 
191 #define	uf_mt				uf_x.x_mt
192 #define	uf_tdb_register_sync		uf_x.x_tdb_register_sync
193 #define	uf_thread_error_detection	uf_x.x_thread_error_detection
194 #define	uf_trs_ted			uf_h.h_trs_ted	/* both of the above */
195 
196 /*
197  * NOTE WELL:
198  * To enable further optimization, the "ul_schedctl_called" member
199  * of the ulwp_t structure (below) serves double-duty:
200  *	1. If NULL, it means that the thread must call __schedctl()
201  *	   to set up its schedctl mappings before acquiring a mutex.
202  *	   This is required by the implementation of adaptive mutex locking.
203  *	2. If non-NULL, it points to uberdata.uberflags, so that tests of
204  *	   uberflags can be made without additional memory references.
205  * This allows the common case of _mutex_lock() and _mutex_unlock() for
206  * USYNC_THREAD mutexes with no error detection and no lock statistics
207  * to be optimized for speed.
208  */
209 
210 /* double the default stack size for 64-bit processes */
211 #ifdef _LP64
212 #define	MINSTACK	(8 * 1024)
213 #define	DEFAULTSTACK	(2 * 1024 * 1024)
214 #else
215 #define	MINSTACK	(4 * 1024)
216 #define	DEFAULTSTACK	(1024 * 1024)
217 #endif
218 
219 #define	MUTEX_TRY	0
220 #define	MUTEX_LOCK	1
221 #define	MUTEX_NOCEIL	0x40
222 
223 #if defined(__x86)
224 
225 typedef struct {	/* structure returned by fnstenv */
226 	int	fctrl;		/* control word */
227 	int	fstat;		/* status word (flags, etc) */
228 	int	ftag;		/* tag of which regs busy */
229 	int	misc[4];	/* other stuff, 28 bytes total */
230 } fpuenv_t;
231 
232 #ifdef _SYSCALL32
233 typedef fpuenv_t fpuenv32_t;
234 #endif	/* _SYSCALL32 */
235 
236 #elif defined(__sparc)
237 
238 typedef struct {	/* fp state structure */
239 	greg_t	fsr;
240 	greg_t	fpu_en;
241 } fpuenv_t;
242 
243 #ifdef _SYSCALL32
244 typedef struct {
245 	greg32_t	fsr;
246 	greg32_t	fpu_en;
247 } fpuenv32_t;
248 #endif	/* _SYSCALL32 */
249 
250 #endif	/* __x86 */
251 
252 #if defined(__x86)
253 extern	void	ht_pause(void);		/* "pause" instruction */
254 #define	SMT_PAUSE()	ht_pause()
255 #else
256 #define	SMT_PAUSE()
257 #endif	/* __x86 */
258 
259 /*
260  * Cleanup handler related data.
261  * This structure is exported as _cleanup_t in pthread.h.
262  * pthread.h exports only the size of this structure, so check
263  * _cleanup_t in pthread.h before making any change here.
264  */
265 typedef struct __cleanup {
266 	struct __cleanup *next;		/* pointer to next handler */
267 	caddr_t	fp;			/* current frame pointer */
268 	void	(*func)(void *);	/* cleanup handler address */
269 	void	*arg;			/* handler's argument */
270 } __cleanup_t;
271 
272 /*
273  * Thread-Specific Data (TSD)
274  * TSD_NFAST includes the invalid key zero, so there
275  * are really only (TSD_NFAST - 1) fast key slots.
276  */
277 typedef	void (*PFrV)(void *);
278 #define	TSD_UNALLOCATED	((PFrV)1)
279 #define	TSD_NFAST	9
280 
281 /*
282  * The tsd union is designed to burn a little memory (9 words) to make
283  * lookups blindingly fast.  Note that tsd_nalloc could be placed at the
284  * end of the pad region to increase the likelihood that it falls on the
285  * same cache line as the data.
286  */
287 typedef union tsd {
288 	uint_t tsd_nalloc;		/* Amount of allocated storage */
289 	void *tsd_pad[TSD_NFAST];
290 	void *tsd_data[1];
291 } tsd_t;
292 
293 typedef struct {
294 	mutex_t tsdm_lock;		/* Lock protecting the data */
295 	uint_t tsdm_nkeys;		/* Number of allocated keys */
296 	uint_t tsdm_nused;		/* Number of used keys */
297 	PFrV *tsdm_destro;		/* Per-key destructors */
298 	char tsdm_pad[64 -		/* pad to 64 bytes */
299 		(sizeof (mutex_t) + 2 * sizeof (uint_t) + sizeof (PFrV *))];
300 } tsd_metadata_t;
301 
302 #ifdef _SYSCALL32
303 typedef union tsd32 {
304 	uint_t tsd_nalloc;		/* Amount of allocated storage */
305 	caddr32_t tsd_pad[TSD_NFAST];
306 	caddr32_t tsd_data[1];
307 } tsd32_t;
308 
309 typedef struct {
310 	mutex_t tsdm_lock;		/* Lock protecting the data */
311 	uint_t tsdm_nkeys;		/* Number of allocated keys */
312 	uint_t tsdm_nused;		/* Number of used keys */
313 	caddr32_t tsdm_destro;		/* Per-key destructors */
314 	char tsdm_pad[64 -		/* pad to 64 bytes */
315 		(sizeof (mutex_t) + 2 * sizeof (uint_t) + sizeof (caddr32_t))];
316 } tsd_metadata32_t;
317 #endif	/* _SYSCALL32 */
318 
319 
320 /*
321  * Thread-Local Storage (TLS)
322  */
323 typedef struct {
324 	void		*tls_data;
325 	size_t		tls_size;
326 } tls_t;
327 
328 typedef struct {
329 	mutex_t	tls_lock;		/* Lock protecting the data */
330 	tls_t	tls_modinfo;		/* Root of all TLS_modinfo data */
331 	tls_t	static_tls;		/* Template for static TLS */
332 	char	tls_pad[64 -		/* pad to 64 bytes */
333 		(sizeof (mutex_t) + 2 * sizeof (tls_t))];
334 } tls_metadata_t;
335 
336 #ifdef _SYSCALL32
337 typedef struct {
338 	caddr32_t	tls_data;
339 	size32_t	tls_size;
340 } tls32_t;
341 
342 typedef struct {
343 	mutex_t	tls_lock;		/* Lock protecting the data */
344 	tls32_t	tls_modinfo;		/* Root of all TLS_modinfo data */
345 	tls32_t	static_tls;		/* Template for static TLS */
346 	char	tls_pad[64 -		/* pad to 64 bytes */
347 		(sizeof (mutex_t) + 2 * sizeof (tls32_t))];
348 } tls_metadata32_t;
349 #endif	/* _SYSCALL32 */
350 
351 
352 /*
353  * Sleep queue root for USYNC_THREAD condvars and mutexes.
354  * There is a default queue root for each queue head (see below).
355  * Also, each ulwp_t contains a queue root that can be used
356  * when the thread is enqueued on the queue, if necessary
357  * (when more than one wchan hashes to the same queue head).
358  */
359 typedef struct queue_root {
360 	struct queue_root	*qr_next;
361 	struct queue_root	*qr_prev;
362 	struct ulwp		*qr_head;
363 	struct ulwp		*qr_tail;
364 	void			*qr_wchan;
365 	uint32_t		qr_rtcount;
366 	uint32_t		qr_qlen;
367 	uint32_t		qr_qmax;
368 } queue_root_t;
369 
370 #ifdef _SYSCALL32
371 typedef struct queue_root32 {
372 	caddr32_t		qr_next;
373 	caddr32_t		qr_prev;
374 	caddr32_t		qr_head;
375 	caddr32_t		qr_tail;
376 	caddr32_t		qr_wchan;
377 	uint32_t		qr_rtcount;
378 	uint32_t		qr_qlen;
379 	uint32_t		qr_qmax;
380 } queue_root32_t;
381 #endif
382 
383 /*
384  * Sleep queue heads for USYNC_THREAD condvars and mutexes.
385  * The size and alignment is 128 bytes to reduce cache conflicts.
386  * Each queue head points to a list of queue roots, defined above.
387  * Each queue head contains a default queue root for use when only one
388  * is needed.  It is always at the tail of the queue root hash chain.
389  */
390 typedef union {
391 	uint64_t		qh_64[16];
392 	struct {
393 		mutex_t		q_lock;
394 		uint8_t		q_qcnt;
395 		uint8_t		q_type;		/* MX or CV */
396 		uint8_t		q_pad1[2];
397 		uint32_t	q_lockcount;
398 		uint32_t	q_qlen;
399 		uint32_t	q_qmax;
400 		void		*q_wchan;	/* valid only while locked */
401 		struct queue_root *q_root;	/* valid only while locked */
402 		struct queue_root *q_hlist;
403 #if !defined(_LP64)
404 		caddr_t		q_pad2[3];
405 #endif
406 		queue_root_t	q_def_root;
407 		uint32_t	q_hlen;
408 		uint32_t	q_hmax;
409 	} qh_qh;
410 } queue_head_t;
411 
412 #define	qh_lock		qh_qh.q_lock
413 #define	qh_qcnt		qh_qh.q_qcnt
414 #define	qh_type		qh_qh.q_type
415 #if defined(THREAD_DEBUG)
416 #define	qh_lockcount	qh_qh.q_lockcount
417 #define	qh_qlen		qh_qh.q_qlen
418 #define	qh_qmax		qh_qh.q_qmax
419 #endif
420 #define	qh_wchan	qh_qh.q_wchan
421 #define	qh_root		qh_qh.q_root
422 #define	qh_hlist	qh_qh.q_hlist
423 #define	qh_def_root	qh_qh.q_def_root
424 #define	qh_hlen		qh_qh.q_hlen
425 #define	qh_hmax		qh_qh.q_hmax
426 
427 /* queue types passed to queue_lock() */
428 #define	MX	0
429 #define	CV	1
430 #define	QHASHSHIFT	9			/* number of hashing bits */
431 #define	QHASHSIZE	(1 << QHASHSHIFT)	/* power of 2 (1<<9 == 512) */
432 #define	QUEUE_HASH(wchan, type)	((uint_t)			\
433 	((((uintptr_t)(wchan) >> 3)				\
434 	^ ((uintptr_t)(wchan) >> (QHASHSHIFT + 3)))		\
435 	& (QHASHSIZE - 1)) + (((type) == MX)? 0 : QHASHSIZE))
436 
437 extern	queue_head_t	*queue_lock(void *, int);
438 extern	void		queue_unlock(queue_head_t *);
439 extern	void		enqueue(queue_head_t *, struct ulwp *, int);
440 extern	struct ulwp	*dequeue(queue_head_t *, int *);
441 extern	struct ulwp	**queue_slot(queue_head_t *, struct ulwp **, int *);
442 extern	struct ulwp	*queue_waiter(queue_head_t *);
443 extern	int		dequeue_self(queue_head_t *);
444 extern	void		queue_unlink(queue_head_t *,
445 				struct ulwp **, struct ulwp *);
446 extern	void		unsleep_self(void);
447 extern	void		spin_lock_set(mutex_t *);
448 extern	void		spin_lock_clear(mutex_t *);
449 
450 /*
451  * Scheduling class information structure.
452  */
453 typedef struct {
454 	short		pcc_state;
455 	short		pcc_policy;
456 	pri_t		pcc_primin;
457 	pri_t		pcc_primax;
458 	pcinfo_t	pcc_info;
459 } pcclass_t;
460 
461 /*
462  * Memory block for chain of owned ceiling mutexes.
463  */
464 typedef struct mxchain {
465 	struct mxchain	*mxchain_next;
466 	mutex_t		*mxchain_mx;
467 } mxchain_t;
468 
469 /*
470  * Pointer to an rwlock that is held for reading.
471  * Used in rw_rdlock() to allow a thread that already holds a read
472  * lock to acquire another read lock on the same rwlock even if
473  * there are writers waiting.  This to avoid deadlock when acquiring
474  * a read lock more than once in the presence of pending writers.
475  * POSIX mandates this behavior.
476  */
477 typedef struct {
478 	void	*rd_rwlock;	/* the rwlock held for reading */
479 	size_t	rd_count;	/* count of read locks applied */
480 } readlock_t;
481 
482 #ifdef _SYSCALL32
483 typedef struct {
484 	caddr32_t	rd_rwlock;
485 	size32_t	rd_count;
486 } readlock32_t;
487 #endif	/* _SYSCALL32 */
488 
489 /*
490  * Maximum number of read locks allowed for one thread on one rwlock.
491  * This could be as large as INT_MAX, but the SUSV3 test suite would
492  * take an inordinately long time to complete.  This is big enough.
493  */
494 #define	READ_LOCK_MAX	100000
495 
496 #define	ul_tlsent	ul_tls.tls_data	/* array of pointers to dynamic TLS */
497 #define	ul_ntlsent	ul_tls.tls_size	/* number of entries in ul_tlsent */
498 
499 /*
500  * Round up an integral value to a multiple of 64
501  */
502 #define	roundup64(x)	(-(-(x) & -64))
503 
504 /*
505  * NOTE:  Whatever changes are made to ulwp_t must be
506  * reflected in $SRC/cmd/mdb/common/modules/libc/libc.c
507  *
508  * NOTE: ul_self *must* be the first member of ulwp_t on x86
509  * Low-level x86 code relies on this.
510  */
511 typedef struct ulwp {
512 	/*
513 	 * These members always need to come first on sparc.
514 	 * For dtrace, a ulwp_t must be aligned on a 64-byte boundary.
515 	 */
516 #if defined(__sparc)
517 	uint32_t	ul_dinstr;	/* scratch space for dtrace */
518 	uint32_t	ul_padsparc0[15];
519 	uint32_t	ul_dsave;	/* dtrace: save %g1, %g0, %sp */
520 	uint32_t	ul_drestore;	/* dtrace: restore %g0, %g0, %g0 */
521 	uint32_t	ul_dftret;	/* dtrace: return probe fasttrap */
522 	uint32_t	ul_dreturn;	/* dtrace: return %o0 */
523 #endif
524 	struct ulwp	*ul_self;	/* pointer to self */
525 #if defined(__i386)
526 	uint8_t		ul_dinstr[40];	/* scratch space for dtrace */
527 #elif defined(__amd64)
528 	uint8_t		ul_dinstr[56];	/* scratch space for dtrace */
529 #endif
530 	struct uberdata *ul_uberdata;	/* uber (super-global) data */
531 	tls_t		ul_tls;		/* dynamic thread-local storage base */
532 	struct ulwp	*ul_forw;	/* forw, back all_lwps list, */
533 	struct ulwp	*ul_back;	/* protected by link_lock */
534 	struct ulwp	*ul_next;	/* list to keep track of stacks */
535 	struct ulwp	*ul_hash;	/* hash chain linked list */
536 	void		*ul_rval;	/* return value from thr_exit() */
537 	caddr_t		ul_stk;		/* mapping base of the stack */
538 	size_t		ul_mapsiz;	/* mapping size of the stack */
539 	size_t		ul_guardsize;	/* normally _lpagesize */
540 	uintptr_t	ul_stktop;	/* broken thr_stksegment() interface */
541 	size_t		ul_stksiz;	/* broken thr_stksegment() interface */
542 	stack_t		ul_ustack;	/* current stack boundaries */
543 	int		ul_ix;		/* hash index */
544 	lwpid_t		ul_lwpid;	/* thread id, aka the lwp id */
545 	pri_t		ul_pri;		/* scheduling priority */
546 	pri_t		ul_epri;	/* real-time ceiling priority */
547 	char		ul_policy;	/* scheduling policy */
548 	char		ul_cid;		/* scheduling class id */
549 	union {
550 		struct {
551 			char	cursig;	/* deferred signal number */
552 			char	pleasestop; /* lwp requested to stop itself */
553 		} s;
554 		short	curplease;	/* for testing both at once */
555 	} ul_cp;
556 	char		ul_stop;	/* reason for stopping */
557 	char		ul_signalled;	/* this lwp was cond_signal()d */
558 	char		ul_dead;	/* this lwp has called thr_exit */
559 	char		ul_unwind;	/* posix: unwind C++ stack */
560 	char		ul_detached;	/* THR_DETACHED at thread_create() */
561 					/* or pthread_detach() was called */
562 	char		ul_writer;	/* sleeping in rw_wrlock() */
563 	char		ul_stopping;	/* set by curthread: stopping self */
564 	char		ul_cancel_prologue;	/* for _cancel_prologue() */
565 	short		ul_preempt;	/* no_preempt()/preempt() */
566 	short		ul_savpreempt;	/* pre-existing preempt value */
567 	char		ul_sigsuspend;	/* thread is in sigsuspend/pollsys */
568 	char		ul_main;	/* thread is the main thread */
569 	char		ul_fork;	/* thread is performing a fork */
570 	char		ul_primarymap;	/* primary link-map is initialized */
571 	/* per-thread copies of the corresponding global variables */
572 	uint8_t		ul_max_spinners;	/* thread_max_spinners */
573 	char		ul_door_noreserve;	/* thread_door_noreserve */
574 	char		ul_queue_fifo;		/* thread_queue_fifo */
575 	char		ul_cond_wait_defer;	/* thread_cond_wait_defer */
576 	char		ul_error_detection;	/* thread_error_detection */
577 	char		ul_async_safe;		/* thread_async_safe */
578 	char		ul_rt;			/* found on an RT queue */
579 	char		ul_rtqueued;		/* was RT when queued */
580 	int		ul_adaptive_spin;	/* thread_adaptive_spin */
581 	int		ul_queue_spin;		/* thread_queue_spin */
582 	volatile int	ul_critical;	/* non-zero == in a critical region */
583 	int		ul_sigdefer;	/* non-zero == defer signals */
584 	int		ul_vfork;	/* thread is the child of vfork() */
585 	int		ul_cancelable;	/* _cancelon()/_canceloff() */
586 	char		ul_cancel_pending;  /* pthread_cancel() was called */
587 	char		ul_cancel_disabled; /* PTHREAD_CANCEL_DISABLE */
588 	char		ul_cancel_async;    /* PTHREAD_CANCEL_ASYNCHRONOUS */
589 	char		ul_save_async;	/* saved copy of ul_cancel_async */
590 	char		ul_mutator;	/* lwp is a mutator (java interface) */
591 	char		ul_created;	/* created suspended */
592 	char		ul_replace;	/* replacement; must be free()d */
593 	uchar_t		ul_nocancel;	/* cancellation can't happen */
594 	int		ul_errno;	/* per-thread errno */
595 	int		*ul_errnop;	/* pointer to errno or self->ul_errno */
596 	__cleanup_t	*ul_clnup_hdr;	/* head of cleanup handlers list */
597 	uberflags_t	*ul_schedctl_called;	/* ul_schedctl is set up */
598 	volatile sc_shared_t *ul_schedctl;	/* schedctl data */
599 	int		ul_bindflags;	/* bind_guard() interface to ld.so.1 */
600 	uint_t		ul_libc_locks;	/* count of cancel_safe_mutex_lock()s */
601 	tsd_t		*ul_stsd;	/* slow TLS for keys >= TSD_NFAST */
602 	void		*ul_ftsd[TSD_NFAST]; /* fast TLS for keys < TSD_NFAST */
603 	td_evbuf_t	ul_td_evbuf;	/* event buffer */
604 	char		ul_td_events_enable;	/* event mechanism enabled */
605 	char		ul_sync_obj_reg;	/* tdb_sync_obj_register() */
606 	char		ul_qtype;	/* MX or CV */
607 	char		ul_cv_wake;	/* != 0: just wake up, don't requeue */
608 	int		ul_usropts;	/* flags given to thr_create() */
609 	void		*(*ul_startpc)(void *); /* start func (thr_create()) */
610 	void		*ul_startarg;	/* argument for start function */
611 	void		*ul_wchan;	/* synch object when sleeping */
612 	struct ulwp	*ul_link;	/* sleep queue link */
613 	queue_head_t	*ul_sleepq;	/* sleep queue thread is waiting on */
614 	mutex_t		*ul_cvmutex;	/* mutex dropped when waiting on a cv */
615 	mxchain_t	*ul_mxchain;	/* chain of owned ceiling mutexes */
616 	int		ul_save_state;	/* bind_guard() interface to ld.so.1 */
617 	uint_t		ul_rdlockcnt;	/* # entries in ul_readlock array */
618 				/* 0 means there is but a single entry */
619 	union {				/* single entry or pointer to array */
620 		readlock_t	single;
621 		readlock_t	*array;
622 	} ul_readlock;
623 	uint_t		ul_heldlockcnt;	/* # entries in ul_heldlocks array */
624 				/* 0 means there is but a single entry */
625 	union {				/* single entry or pointer to array */
626 		mutex_t		*single;
627 		mutex_t		**array;
628 	} ul_heldlocks;
629 	/* PROBE_SUPPORT begin */
630 	void		*ul_tpdp;
631 	/* PROBE_SUPPORT end */
632 	ucontext_t	*ul_siglink;	/* pointer to previous context */
633 	uint_t		ul_spin_lock_spin;	/* spin lock statistics */
634 	uint_t		ul_spin_lock_spin2;
635 	uint_t		ul_spin_lock_sleep;
636 	uint_t		ul_spin_lock_wakeup;
637 	queue_root_t	ul_queue_root;	/* root of a sleep queue */
638 	id_t		ul_rtclassid;	/* real-time class id */
639 	uint_t		ul_pilocks;	/* count of PI locks held */
640 		/* the following members *must* be last in the structure */
641 		/* they are discarded when ulwp is replaced on thr_exit() */
642 	sigset_t	ul_sigmask;	/* thread's current signal mask */
643 	sigset_t	ul_tmpmask;	/* signal mask for sigsuspend/pollsys */
644 	siginfo_t	ul_siginfo;	/* deferred siginfo */
645 	mutex_t		ul_spinlock;	/* used when suspending/continuing */
646 	fpuenv_t	ul_fpuenv;	/* floating point state */
647 	uintptr_t	ul_sp;		/* stack pointer when blocked */
648 	void		*ul_ex_unwind;	/* address of _ex_unwind() or -1 */
649 #if defined(sparc)
650 	void		*ul_unwind_ret;	/* used only by _ex_clnup_handler() */
651 #endif
652 } ulwp_t;
653 
654 #define	ul_cursig	ul_cp.s.cursig		/* deferred signal number */
655 #define	ul_pleasestop	ul_cp.s.pleasestop	/* lwp requested to stop */
656 #define	ul_curplease	ul_cp.curplease		/* for testing both at once */
657 
658 /*
659  * This is the size of a replacement ulwp, retained only for the benefit
660  * of thr_join().  The trailing members are unneeded for this purpose.
661  */
662 #define	REPLACEMENT_SIZE	((size_t)&((ulwp_t *)NULL)->ul_sigmask)
663 
664 /*
665  * Definitions for static initialization of signal sets,
666  * plus some sneaky optimizations in various places.
667  */
668 
669 #define	SIGMASK(sig)	((uint32_t)1 << (((sig) - 1) & (32 - 1)))
670 
671 #if (MAXSIG > 32 && MAXSIG <= 64)
672 #define	FILLSET0	0xffffffffu
673 #define	FILLSET1	((1u << (MAXSIG - 32)) - 1)
674 #else
675 #error "fix me: MAXSIG out of bounds"
676 #endif
677 
678 #define	CANTMASK0	(SIGMASK(SIGKILL) | SIGMASK(SIGSTOP))
679 #define	CANTMASK1	0
680 
681 #define	MASKSET0	(FILLSET0 & ~CANTMASK0)
682 #define	MASKSET1	(FILLSET1 & ~CANTMASK1)
683 
684 extern	const sigset_t maskset;		/* set of all maskable signals */
685 
686 extern	int	thread_adaptive_spin;
687 extern	uint_t	thread_max_spinners;
688 extern	int	thread_queue_spin;
689 extern	int	thread_queue_fifo;
690 extern	int	thread_queue_dump;
691 extern	int	thread_cond_wait_defer;
692 extern	int	thread_async_safe;
693 extern	int	thread_queue_verify;
694 
695 /*
696  * pthread_atfork() related data, used to store atfork handlers.
697  */
698 typedef struct atfork {
699 	struct atfork *forw;		/* forward pointer */
700 	struct atfork *back;		/* backward pointer */
701 	void (*prepare)(void);		/* pre-fork handler */
702 	void (*parent)(void);		/* post-fork parent handler */
703 	void (*child)(void);		/* post-fork child handler */
704 } atfork_t;
705 
706 /*
707  * Element in the table of registered process robust locks.
708  * We keep track of these to make sure that we only call
709  * ___lwp_mutex_register() once for each such lock.
710  */
711 typedef struct robust {
712 	struct robust	*robust_next;
713 	mutex_t		*robust_lock;
714 } robust_t;
715 
716 /*
717  * Parameters of the lock registration hash table.
718  */
719 #define	LOCKSHIFT	9			/* number of hashing bits */
720 #define	LOCKHASHSZ	(1 << LOCKSHIFT)	/* power of 2 (1<<9 == 512) */
721 #define	LOCK_HASH(addr)	(uint_t)			\
722 	((((uintptr_t)(addr) >> 3)			\
723 	^ ((uintptr_t)(addr) >> (LOCKSHIFT + 3)))	\
724 	& (LOCKHASHSZ - 1))
725 
726 /*
727  * Make our hot locks reside on private cache lines (64 bytes).
728  */
729 typedef struct {
730 	mutex_t	pad_lock;
731 	char	pad_pad[64 - sizeof (mutex_t)];
732 } pad_lock_t;
733 
734 /*
735  * Make our semi-hot locks reside on semi-private cache lines (32 bytes).
736  */
737 typedef struct {
738 	mutex_t	pad_lock;
739 	char	pad_pad[32 - sizeof (mutex_t)];
740 } pad32_lock_t;
741 
742 /*
743  * The threads hash table is used for fast lookup and locking of an active
744  * thread structure (ulwp_t) given a thread-id.  It is an N-element array of
745  * thr_hash_table_t structures, where N == 1 before the main thread creates
746  * the first additional thread and N == 1024 afterwards.  Each element of the
747  * table is 64 bytes in size and alignment to reduce cache conflicts.
748  */
749 typedef struct {
750 	mutex_t	hash_lock;	/* lock per bucket */
751 	cond_t	hash_cond;	/* convar per bucket */
752 	ulwp_t	*hash_bucket;	/* hash bucket points to the list of ulwps */
753 	char	hash_pad[64 -	/* pad out to 64 bytes */
754 		(sizeof (mutex_t) + sizeof (cond_t) + sizeof (ulwp_t *))];
755 } thr_hash_table_t;
756 
757 #ifdef _SYSCALL32
758 typedef struct {
759 	mutex_t	hash_lock;
760 	cond_t	hash_cond;
761 	caddr32_t hash_bucket;
762 	char	hash_pad[64 -
763 		(sizeof (mutex_t) + sizeof (cond_t) + sizeof (caddr32_t))];
764 } thr_hash_table32_t;
765 #endif	/* _SYSCALL32 */
766 
767 
768 /*
769  * siguaction members have 128-byte size and 64-byte alignment.
770  * We know that sizeof (struct sigaction) is 32 bytes for both
771  * _ILP32 and _LP64 and that sizeof (rwlock_t) is 64 bytes.
772  */
773 typedef struct {
774 	rwlock_t	sig_lock;
775 	struct sigaction sig_uaction;
776 	char	sig_pad[128 - sizeof (rwlock_t) - sizeof (struct sigaction)];
777 } siguaction_t;
778 
779 #ifdef _SYSCALL32
780 typedef struct {
781 	rwlock_t	sig_lock;
782 	struct sigaction32 sig_uaction;
783 	char	sig_pad[128 - sizeof (rwlock_t) - sizeof (struct sigaction32)];
784 } siguaction32_t;
785 #endif	/* _SYSCALL32 */
786 
787 
788 /*
789  * Bucket structures, used by lmalloc()/lfree().
790  * See port/threads/alloc.c for details.
791  * A bucket's size and alignment is 64 bytes.
792  */
793 typedef struct {
794 	mutex_t	bucket_lock;	/* protects the free list allocations */
795 	void	*free_list;	/* LIFO list of blocks to allocate/free */
796 	size_t	chunks;		/* number of 64K blocks mmap()ed last time */
797 	char	pad64[64 -	/* pad out to 64 bytes */
798 		(sizeof (mutex_t) + sizeof (void *) + sizeof (size_t))];
799 } bucket_t;
800 
801 #ifdef _SYSCALL32
802 typedef struct {
803 	mutex_t		bucket_lock;
804 	caddr32_t	free_list;
805 	size32_t	chunks;
806 	char	pad64[64 -	/* pad out to 64 bytes */
807 		(sizeof (mutex_t) + sizeof (caddr32_t) + sizeof (size32_t))];
808 } bucket32_t;
809 #endif	/* _SYSCALL32 */
810 
811 #define	NBUCKETS	10	/* sizes ranging from 64 to 32768 */
812 
813 
814 /*
815  * atexit() data structures.
816  * See port/gen/atexit.c for details.
817  */
818 typedef void (*_exithdlr_func_t) (void);
819 
820 typedef struct _exthdlr {
821 	struct _exthdlr 	*next;	/* next in handler list */
822 	_exithdlr_func_t	hdlr;	/* handler itself */
823 } _exthdlr_t;
824 
825 typedef struct {
826 	mutex_t		exitfns_lock;
827 	_exthdlr_t	*head;
828 	void		*exit_frame_monitor;
829 	char		exit_pad[64 -	/* pad out to 64 bytes */
830 		(sizeof (mutex_t) + sizeof (_exthdlr_t *) + sizeof (void *))];
831 } atexit_root_t;
832 
833 #ifdef _SYSCALL32
834 typedef struct {
835 	mutex_t		exitfns_lock;
836 	caddr32_t	head;
837 	caddr32_t	exit_frame_monitor;
838 	char		exit_pad[64 -	/* pad out to 64 bytes */
839 		(sizeof (mutex_t) + sizeof (caddr32_t) + sizeof (caddr32_t))];
840 } atexit_root32_t;
841 #endif	/* _SYSCALL32 */
842 
843 
844 /*
845  * This is data that is global to all link maps (uberdata, aka super-global).
846  */
847 typedef struct uberdata {
848 	pad_lock_t	_link_lock;
849 	pad_lock_t	_ld_lock;
850 	pad_lock_t	_fork_lock;
851 	pad_lock_t	_atfork_lock;
852 	pad32_lock_t	_callout_lock;
853 	pad32_lock_t	_tdb_hash_lock;
854 	tdb_sync_stats_t tdb_hash_lock_stats;
855 	siguaction_t	siguaction[NSIG];
856 	bucket_t	bucket[NBUCKETS];
857 	atexit_root_t	atexit_root;
858 	tsd_metadata_t	tsd_metadata;
859 	tls_metadata_t	tls_metadata;
860 	/*
861 	 * Every object before this point has size and alignment of 64 bytes.
862 	 * Don't add any other type of data before this point.
863 	 */
864 	char	primary_map;	/* set when primary link map is initialized */
865 	char	bucket_init;	/* set when bucket[NBUCKETS] is initialized */
866 	char	pad[2];
867 	uberflags_t	uberflags;
868 	queue_head_t	*queue_head;
869 	thr_hash_table_t *thr_hash_table;
870 	uint_t		hash_size;	/* # of entries in thr_hash_table[] */
871 	uint_t		hash_mask;	/* hash_size - 1 */
872 	ulwp_t	*ulwp_one;	/* main thread */
873 	ulwp_t	*all_lwps;	/* circular ul_forw/ul_back list of live lwps */
874 	ulwp_t	*all_zombies;	/* circular ul_forw/ul_back list of zombies */
875 	int	nthreads;	/* total number of live threads/lwps */
876 	int	nzombies;	/* total number of zombie threads */
877 	int	ndaemons;	/* total number of THR_DAEMON threads/lwps */
878 	pid_t	pid;		/* the current process's pid */
879 	void	(*sigacthandler)(int, siginfo_t *, void *);
880 	ulwp_t	*lwp_stacks;
881 	ulwp_t	*lwp_laststack;
882 	int	nfreestack;
883 	int	thread_stack_cache;
884 	ulwp_t	*ulwp_freelist;
885 	ulwp_t	*ulwp_lastfree;
886 	ulwp_t	*ulwp_replace_free;
887 	ulwp_t	*ulwp_replace_last;
888 	atfork_t	*atforklist;	/* circular Q for fork handlers */
889 	robust_t	**robustlocks;	/* table of registered robust locks */
890 	struct uberdata **tdb_bootstrap;
891 	tdb_t	tdb;		/* thread debug interfaces (for libc_db) */
892 } uberdata_t;
893 
894 #define	link_lock	_link_lock.pad_lock
895 #define	ld_lock		_ld_lock.pad_lock
896 #define	fork_lock	_fork_lock.pad_lock
897 #define	atfork_lock	_atfork_lock.pad_lock
898 #define	callout_lock	_callout_lock.pad_lock
899 #define	tdb_hash_lock	_tdb_hash_lock.pad_lock
900 
901 #pragma align 64(__uberdata)
902 extern	uberdata_t	__uberdata;
903 extern	uberdata_t	**__tdb_bootstrap;	/* known to libc_db and mdb */
904 extern	int		primary_link_map;
905 
906 #define	ulwp_mutex(ulwp, udp)	\
907 	(&(udp)->thr_hash_table[(ulwp)->ul_ix].hash_lock)
908 #define	ulwp_condvar(ulwp, udp)	\
909 	(&(udp)->thr_hash_table[(ulwp)->ul_ix].hash_cond)
910 
911 /*
912  * Grab and release the hash table lock for the specified lwp.
913  */
914 #define	ulwp_lock(ulwp, udp)	lmutex_lock(ulwp_mutex(ulwp, udp))
915 #define	ulwp_unlock(ulwp, udp)	lmutex_unlock(ulwp_mutex(ulwp, udp))
916 
917 #ifdef _SYSCALL32	/* needed by libc_db */
918 
919 typedef struct ulwp32 {
920 #if defined(__sparc)
921 	uint32_t	ul_dinstr;	/* scratch space for dtrace */
922 	uint32_t	ul_padsparc0[15];
923 	uint32_t	ul_dsave;	/* dtrace: save %g1, %g0, %sp */
924 	uint32_t	ul_drestore;	/* dtrace: restore %g0, %g0, %g0 */
925 	uint32_t	ul_dftret;	/* dtrace: return probe fasttrap */
926 	uint32_t	ul_dreturn;	/* dtrace: return %o0 */
927 #endif
928 	caddr32_t	ul_self;	/* pointer to self */
929 #if defined(__x86)
930 	uint8_t		ul_dinstr[40];	/* scratch space for dtrace */
931 #endif
932 	caddr32_t	ul_uberdata;	/* uber (super-global) data */
933 	tls32_t		ul_tls;		/* dynamic thread-local storage base */
934 	caddr32_t	ul_forw;	/* forw, back all_lwps list, */
935 	caddr32_t	ul_back;	/* protected by link_lock */
936 	caddr32_t	ul_next;	/* list to keep track of stacks */
937 	caddr32_t	ul_hash;	/* hash chain linked list */
938 	caddr32_t	ul_rval;	/* return value from thr_exit() */
939 	caddr32_t	ul_stk;		/* mapping base of the stack */
940 	size32_t	ul_mapsiz;	/* mapping size of the stack */
941 	size32_t	ul_guardsize;	/* normally _lpagesize */
942 	caddr32_t	ul_stktop;	/* broken thr_stksegment() interface */
943 	size32_t	ul_stksiz;	/* broken thr_stksegment() interface */
944 	stack32_t	ul_ustack;	/* current stack boundaries */
945 	int		ul_ix;		/* hash index */
946 	lwpid_t		ul_lwpid;	/* thread id, aka the lwp id */
947 	pri_t		ul_pri;		/* scheduling priority */
948 	pri_t		ul_epri;	/* real-time ceiling priority */
949 	char		ul_policy;	/* scheduling policy */
950 	char		ul_cid;		/* scheduling class id */
951 	union {
952 		struct {
953 			char	cursig;	/* deferred signal number */
954 			char	pleasestop; /* lwp requested to stop itself */
955 		} s;
956 		short	curplease;	/* for testing both at once */
957 	} ul_cp;
958 	char		ul_stop;	/* reason for stopping */
959 	char		ul_signalled;	/* this lwp was cond_signal()d */
960 	char		ul_dead;	/* this lwp has called thr_exit */
961 	char		ul_unwind;	/* posix: unwind C++ stack */
962 	char		ul_detached;	/* THR_DETACHED at thread_create() */
963 					/* or pthread_detach() was called */
964 	char		ul_writer;	/* sleeping in rw_wrlock() */
965 	char		ul_stopping;	/* set by curthread: stopping self */
966 	char		ul_cancel_prologue;	/* for _cancel_prologue() */
967 	short		ul_preempt;	/* no_preempt()/preempt() */
968 	short		ul_savpreempt;	/* pre-existing preempt value */
969 	char		ul_sigsuspend;	/* thread is in sigsuspend/pollsys */
970 	char		ul_main;	/* thread is the main thread */
971 	char		ul_fork;	/* thread is performing a fork */
972 	char		ul_primarymap;	/* primary link-map is initialized */
973 	/* per-thread copies of the corresponding global variables */
974 	uint8_t		ul_max_spinners;	/* thread_max_spinners */
975 	char		ul_door_noreserve;	/* thread_door_noreserve */
976 	char		ul_queue_fifo;		/* thread_queue_fifo */
977 	char		ul_cond_wait_defer;	/* thread_cond_wait_defer */
978 	char		ul_error_detection;	/* thread_error_detection */
979 	char		ul_async_safe;		/* thread_async_safe */
980 	char		ul_rt;			/* found on an RT queue */
981 	char		ul_rtqueued;		/* was RT when queued */
982 	int		ul_adaptive_spin;	/* thread_adaptive_spin */
983 	int		ul_queue_spin;		/* thread_queue_spin */
984 	int		ul_critical;	/* non-zero == in a critical region */
985 	int		ul_sigdefer;	/* non-zero == defer signals */
986 	int		ul_vfork;	/* thread is the child of vfork() */
987 	int		ul_cancelable;	/* _cancelon()/_canceloff() */
988 	char		ul_cancel_pending;  /* pthread_cancel() was called */
989 	char		ul_cancel_disabled; /* PTHREAD_CANCEL_DISABLE */
990 	char		ul_cancel_async;    /* PTHREAD_CANCEL_ASYNCHRONOUS */
991 	char		ul_save_async;	/* saved copy of ul_cancel_async */
992 	char		ul_mutator;	/* lwp is a mutator (java interface) */
993 	char		ul_created;	/* created suspended */
994 	char		ul_replace;	/* replacement; must be free()d */
995 	uchar_t		ul_nocancel;	/* cancellation can't happen */
996 	int		ul_errno;	/* per-thread errno */
997 	caddr32_t	ul_errnop;	/* pointer to errno or self->ul_errno */
998 	caddr32_t	ul_clnup_hdr;	/* head of cleanup handlers list */
999 	caddr32_t	ul_schedctl_called; /* ul_schedctl is set up */
1000 	caddr32_t	ul_schedctl;	/* schedctl data */
1001 	int		ul_bindflags;	/* bind_guard() interface to ld.so.1 */
1002 	uint_t		ul_libc_locks;	/* count of cancel_safe_mutex_lock()s */
1003 	caddr32_t	ul_stsd;	/* slow TLS for keys >= TSD_NFAST */
1004 	caddr32_t	ul_ftsd[TSD_NFAST]; /* fast TLS for keys < TSD_NFAST */
1005 	td_evbuf32_t	ul_td_evbuf;	/* event buffer */
1006 	char		ul_td_events_enable;	/* event mechanism enabled */
1007 	char		ul_sync_obj_reg;	/* tdb_sync_obj_register() */
1008 	char		ul_qtype;	/* MX or CV */
1009 	char		ul_cv_wake;	/* != 0: just wake up, don't requeue */
1010 	int		ul_usropts;	/* flags given to thr_create() */
1011 	caddr32_t	ul_startpc;	/* start func (thr_create()) */
1012 	caddr32_t	ul_startarg;	/* argument for start function */
1013 	caddr32_t	ul_wchan;	/* synch object when sleeping */
1014 	caddr32_t	ul_link;	/* sleep queue link */
1015 	caddr32_t	ul_sleepq;	/* sleep queue thread is waiting on */
1016 	caddr32_t	ul_cvmutex;	/* mutex dropped when waiting on a cv */
1017 	caddr32_t	ul_mxchain;	/* chain of owned ceiling mutexes */
1018 	int		ul_save_state;	/* bind_guard() interface to ld.so.1 */
1019 	uint_t		ul_rdlockcnt;	/* # entries in ul_readlock array */
1020 				/* 0 means there is but a single entry */
1021 	union {				/* single entry or pointer to array */
1022 		readlock32_t	single;
1023 		caddr32_t	array;
1024 	} ul_readlock;
1025 	uint_t		ul_heldlockcnt;	/* # entries in ul_heldlocks array */
1026 				/* 0 means there is but a single entry */
1027 	union {				/* single entry or pointer to array */
1028 		caddr32_t	single;
1029 		caddr32_t	array;
1030 	} ul_heldlocks;
1031 	/* PROBE_SUPPORT begin */
1032 	caddr32_t	ul_tpdp;
1033 	/* PROBE_SUPPORT end */
1034 	caddr32_t	ul_siglink;	/* pointer to previous context */
1035 	uint_t		ul_spin_lock_spin;	/* spin lock statistics */
1036 	uint_t		ul_spin_lock_spin2;
1037 	uint_t		ul_spin_lock_sleep;
1038 	uint_t		ul_spin_lock_wakeup;
1039 	queue_root32_t	ul_queue_root;	/* root of a sleep queue */
1040 	id_t		ul_rtclassid;	/* real-time class id */
1041 	uint_t		ul_pilocks;	/* count of PI locks held */
1042 		/* the following members *must* be last in the structure */
1043 		/* they are discarded when ulwp is replaced on thr_exit() */
1044 	sigset32_t	ul_sigmask;	/* thread's current signal mask */
1045 	sigset32_t	ul_tmpmask;	/* signal mask for sigsuspend/pollsys */
1046 	siginfo32_t	ul_siginfo;	/* deferred siginfo */
1047 	mutex_t		ul_spinlock;	/* used when suspending/continuing */
1048 	fpuenv32_t	ul_fpuenv;	/* floating point state */
1049 	caddr32_t	ul_sp;		/* stack pointer when blocked */
1050 #if defined(sparc)
1051 	caddr32_t	ul_unwind_ret;	/* used only by _ex_clnup_handler() */
1052 #endif
1053 } ulwp32_t;
1054 
1055 #define	REPLACEMENT_SIZE32	((size_t)&((ulwp32_t *)NULL)->ul_sigmask)
1056 
1057 typedef struct uberdata32 {
1058 	pad_lock_t	_link_lock;
1059 	pad_lock_t	_ld_lock;
1060 	pad_lock_t	_fork_lock;
1061 	pad_lock_t	_atfork_lock;
1062 	pad32_lock_t	_callout_lock;
1063 	pad32_lock_t	_tdb_hash_lock;
1064 	tdb_sync_stats_t tdb_hash_lock_stats;
1065 	siguaction32_t	siguaction[NSIG];
1066 	bucket32_t	bucket[NBUCKETS];
1067 	atexit_root32_t	atexit_root;
1068 	tsd_metadata32_t tsd_metadata;
1069 	tls_metadata32_t tls_metadata;
1070 	char		primary_map;
1071 	char		bucket_init;
1072 	char		pad[2];
1073 	uberflags_t	uberflags;
1074 	caddr32_t	queue_head;
1075 	caddr32_t	thr_hash_table;
1076 	uint_t		hash_size;
1077 	uint_t		hash_mask;
1078 	caddr32_t	ulwp_one;
1079 	caddr32_t	all_lwps;
1080 	caddr32_t	all_zombies;
1081 	int		nthreads;
1082 	int		nzombies;
1083 	int		ndaemons;
1084 	int		pid;
1085 	caddr32_t	sigacthandler;
1086 	caddr32_t	lwp_stacks;
1087 	caddr32_t	lwp_laststack;
1088 	int		nfreestack;
1089 	int		thread_stack_cache;
1090 	caddr32_t	ulwp_freelist;
1091 	caddr32_t	ulwp_lastfree;
1092 	caddr32_t	ulwp_replace_free;
1093 	caddr32_t	ulwp_replace_last;
1094 	caddr32_t	atforklist;
1095 	caddr32_t	robustlocks;
1096 	caddr32_t	tdb_bootstrap;
1097 	tdb32_t		tdb;
1098 } uberdata32_t;
1099 
1100 #endif	/* _SYSCALL32 */
1101 
1102 /* ul_stop values */
1103 #define	TSTP_REGULAR	0x01	/* Stopped by thr_suspend() */
1104 #define	TSTP_MUTATOR	0x08	/* stopped by thr_suspend_*mutator*() */
1105 #define	TSTP_FORK	0x20	/* stopped by suspend_fork() */
1106 
1107 /*
1108  * Implementation-specific attribute types for pthread_mutexattr_init() etc.
1109  */
1110 
1111 typedef	struct	_cvattr {
1112 	int	pshared;
1113 	clockid_t clockid;
1114 } cvattr_t;
1115 
1116 typedef	struct	_mattr {
1117 	int	pshared;
1118 	int	protocol;
1119 	int	prioceiling;
1120 	int	type;
1121 	int	robustness;
1122 } mattr_t;
1123 
1124 typedef	struct	_thrattr {
1125 	size_t	stksize;
1126 	void	*stkaddr;
1127 	int	detachstate;
1128 	int	daemonstate;
1129 	int	scope;
1130 	int	prio;
1131 	int	policy;
1132 	int	inherit;
1133 	size_t	guardsize;
1134 } thrattr_t;
1135 
1136 typedef	struct	_rwlattr {
1137 	int	pshared;
1138 } rwlattr_t;
1139 
1140 /* _curthread() is inline for speed */
1141 extern	ulwp_t		*_curthread(void);
1142 #define	curthread	(_curthread())
1143 
1144 /* this version (also inline) can be tested for NULL */
1145 extern	ulwp_t		*__curthread(void);
1146 
1147 /* get the current stack pointer (also inline) */
1148 extern	greg_t		stkptr(void);
1149 
1150 /*
1151  * Suppress __attribute__((...)) if we are not compiling with gcc
1152  */
1153 #if !defined(__GNUC__)
1154 #define	__attribute__(string)
1155 #endif
1156 
1157 /* Fetch the dispatch (kernel) priority of a thread */
1158 #define	real_priority(ulwp)	\
1159 	((ulwp)->ul_schedctl? (ulwp)->ul_schedctl->sc_priority : 0)
1160 
1161 /*
1162  * Implementation functions.  Not visible outside of the library itself.
1163  */
1164 extern	int	__nanosleep(const timespec_t *, timespec_t *);
1165 extern	void	getgregs(ulwp_t *, gregset_t);
1166 extern	void	setgregs(ulwp_t *, gregset_t);
1167 extern	void	thr_panic(const char *);
1168 #pragma rarely_called(thr_panic)
1169 extern	ulwp_t	*find_lwp(thread_t);
1170 extern	void	finish_init(void);
1171 extern	void	update_sched(ulwp_t *);
1172 extern	void	queue_alloc(void);
1173 extern	void	tsd_exit(void);
1174 extern	void	tsd_free(ulwp_t *);
1175 extern	void	tls_setup(void);
1176 extern	void	tls_exit(void);
1177 extern	void	tls_free(ulwp_t *);
1178 extern	void	rwl_free(ulwp_t *);
1179 extern	void	heldlock_exit(void);
1180 extern	void	heldlock_free(ulwp_t *);
1181 extern	void	sigacthandler(int, siginfo_t *, void *);
1182 extern	void	signal_init(void);
1183 extern	int	sigequalset(const sigset_t *, const sigset_t *);
1184 extern	void	mutex_setup(void);
1185 extern	void	take_deferred_signal(int);
1186 extern	int	setup_context(ucontext_t *, void *(*func)(ulwp_t *),
1187 			ulwp_t *ulwp, caddr_t stk, size_t stksize);
1188 extern	volatile sc_shared_t *setup_schedctl(void);
1189 extern	void	*lmalloc(size_t);
1190 extern	void	lfree(void *, size_t);
1191 extern	void	*libc_malloc(size_t);
1192 extern	void	*libc_realloc(void *, size_t);
1193 extern	void	libc_free(void *);
1194 extern	char	*libc_strdup(const char *);
1195 extern	void	ultos(uint64_t, int, char *);
1196 extern	void	lock_error(const mutex_t *, const char *, void *, const char *);
1197 extern	void	rwlock_error(const rwlock_t *, const char *, const char *);
1198 extern	void	thread_error(const char *);
1199 extern	void	grab_assert_lock(void);
1200 extern	void	dump_queue_statistics(void);
1201 extern	void	collect_queue_statistics(void);
1202 extern	void	record_spin_locks(ulwp_t *);
1203 extern	void	remember_lock(mutex_t *);
1204 extern	void	forget_lock(mutex_t *);
1205 extern	void	register_lock(mutex_t *);
1206 extern	void	unregister_locks(void);
1207 #if defined(__sparc)
1208 extern	void	_flush_windows(void);
1209 #else
1210 #define	_flush_windows()
1211 #endif
1212 extern	void	set_curthread(void *);
1213 
1214 /*
1215  * Utility function used when waking up many threads (more than MAXLWPS)
1216  * all at once.  See mutex_wakeup_all(), cond_broadcast(), and rw_unlock().
1217  */
1218 #define	MAXLWPS	128	/* max remembered lwpids before overflow */
1219 #define	NEWLWPS	2048	/* max remembered lwpids at first overflow */
1220 extern	lwpid_t	*alloc_lwpids(lwpid_t *, int *, int *);
1221 
1222 /* enter a critical section */
1223 #define	enter_critical(self)	(self->ul_critical++)
1224 
1225 /* exit a critical section, take deferred actions if necessary */
1226 extern	void	do_exit_critical(void);
1227 #define	exit_critical(self)					\
1228 	(void) (self->ul_critical--,				\
1229 	    ((self->ul_curplease && self->ul_critical == 0)?	\
1230 	    (do_exit_critical(), 0) : 0))
1231 
1232 /*
1233  * Like enter_critical()/exit_critical() but just for deferring signals.
1234  * Unlike enter_critical()/exit_critical(), ul_sigdefer may be set while
1235  * calling application functions like constructors and destructors.
1236  * Care must be taken if the application function attempts to set
1237  * the signal mask while a deferred signal is present; the setting
1238  * of the signal mask must also be deferred.
1239  */
1240 #define	sigoff(self)	(self->ul_sigdefer++)
1241 extern	void	sigon(ulwp_t *);
1242 
1243 /* these are exported functions */
1244 extern	void	_sigoff(void);
1245 extern	void	_sigon(void);
1246 
1247 #define	sigorset(s1, s2)				\
1248 	(((s1)->__sigbits[0] |= (s2)->__sigbits[0]),	\
1249 	((s1)->__sigbits[1] |= (s2)->__sigbits[1]),	\
1250 	((s1)->__sigbits[2] |= (s2)->__sigbits[2]),	\
1251 	((s1)->__sigbits[3] |= (s2)->__sigbits[3]))
1252 
1253 #define	sigandset(s1, s2)				\
1254 	(((s1)->__sigbits[0] &= (s2)->__sigbits[0]),	\
1255 	((s1)->__sigbits[1] &= (s2)->__sigbits[1]),	\
1256 	((s1)->__sigbits[2] &= (s2)->__sigbits[2]),	\
1257 	((s1)->__sigbits[3] &= (s2)->__sigbits[3]))
1258 
1259 #define	sigdiffset(s1, s2)				\
1260 	(((s1)->__sigbits[0] &= ~(s2)->__sigbits[0]),	\
1261 	((s1)->__sigbits[1] &= ~(s2)->__sigbits[1]),	\
1262 	((s1)->__sigbits[2] &= ~(s2)->__sigbits[2]),	\
1263 	((s1)->__sigbits[3] &= ~(s2)->__sigbits[3]))
1264 
1265 #define	delete_reserved_signals(s)			\
1266 	(((s)->__sigbits[0] &= MASKSET0),		\
1267 	((s)->__sigbits[1] &= (MASKSET1 & ~SIGMASK(SIGCANCEL))),\
1268 	((s)->__sigbits[2] = 0),			\
1269 	((s)->__sigbits[3] = 0))
1270 
1271 extern	void	block_all_signals(ulwp_t *self);
1272 
1273 /*
1274  * When restoring the signal mask after having previously called
1275  * block_all_signals(), if we have a deferred signal present then
1276  * do nothing other than ASSERT() that we are in a critical region.
1277  * The signal mask will be set when we emerge from the critical region
1278  * and call take_deferred_signal().  There is no race condition here
1279  * because the kernel currently has all signals blocked for this thread.
1280  */
1281 #define	restore_signals(self)						\
1282 	((void) ((self)->ul_cursig?					\
1283 	(ASSERT((self)->ul_critical + (self)->ul_sigdefer != 0), 0) :	\
1284 	__lwp_sigmask(SIG_SETMASK, &(self)->ul_sigmask, NULL)))
1285 
1286 extern	void	set_cancel_pending_flag(ulwp_t *, int);
1287 extern	void	set_cancel_eintr_flag(ulwp_t *);
1288 extern	void	set_parking_flag(ulwp_t *, int);
1289 extern	int	cancel_active(void);
1290 
1291 extern	void	*_thr_setup(ulwp_t *);
1292 extern	void	_fpinherit(ulwp_t *);
1293 extern	void	_lwp_start(void);
1294 extern	void	_lwp_terminate(void);
1295 extern	void	lmutex_lock(mutex_t *);
1296 extern	void	lmutex_unlock(mutex_t *);
1297 extern	void	lrw_rdlock(rwlock_t *);
1298 extern	void	lrw_wrlock(rwlock_t *);
1299 extern	void	lrw_unlock(rwlock_t *);
1300 extern	void	sig_mutex_lock(mutex_t *);
1301 extern	void	sig_mutex_unlock(mutex_t *);
1302 extern	int	sig_mutex_trylock(mutex_t *);
1303 extern	int	sig_cond_wait(cond_t *, mutex_t *);
1304 extern	int	sig_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
1305 extern	void	cancel_safe_mutex_lock(mutex_t *);
1306 extern	void	cancel_safe_mutex_unlock(mutex_t *);
1307 extern	int	cancel_safe_mutex_trylock(mutex_t *);
1308 extern	void	_prefork_handler(void);
1309 extern	void	_postfork_parent_handler(void);
1310 extern	void	_postfork_child_handler(void);
1311 extern	void	postfork1_child(void);
1312 extern	void	postfork1_child_aio(void);
1313 extern	void	postfork1_child_sigev_aio(void);
1314 extern	void	postfork1_child_sigev_mq(void);
1315 extern	void	postfork1_child_sigev_timer(void);
1316 extern	void	postfork1_child_tpool(void);
1317 extern	void	fork_lock_enter(void);
1318 extern	void	fork_lock_exit(void);
1319 extern	void	suspend_fork(void);
1320 extern	void	continue_fork(int);
1321 extern	void	do_sigcancel(void);
1322 extern	void	setup_cancelsig(int);
1323 extern	void	init_sigev_thread(void);
1324 extern	void	init_aio(void);
1325 extern	void	_cancelon(void);
1326 extern	void	_canceloff(void);
1327 extern	void	_canceloff_nocancel(void);
1328 extern	void	_cancel_prologue(void);
1329 extern	void	_cancel_epilogue(void);
1330 extern	void	no_preempt(ulwp_t *);
1331 extern	void	preempt(ulwp_t *);
1332 extern	void	_thrp_unwind(void *);
1333 
1334 extern	pid_t	__forkx(int);
1335 extern	pid_t	__forkallx(int);
1336 extern	int	_kill(pid_t, int);
1337 extern	int	__open(const char *, int, ...);
1338 extern	int	__close(int);
1339 extern	ssize_t	__read(int, void *, size_t);
1340 extern	ssize_t	__write(int, const void *, size_t);
1341 extern	int	__fcntl(int, int, ...);
1342 extern	int	__lwp_continue(lwpid_t);
1343 extern	int	__lwp_create(ucontext_t *, uint_t, lwpid_t *);
1344 extern	int	__lwp_kill(lwpid_t, int);
1345 extern	lwpid_t	__lwp_self(void);
1346 extern	int	___lwp_suspend(lwpid_t);
1347 extern	int	lwp_wait(lwpid_t, lwpid_t *);
1348 extern	int	__lwp_wait(lwpid_t, lwpid_t *);
1349 extern	int	__lwp_detach(lwpid_t);
1350 extern	sc_shared_t *__schedctl(void);
1351 
1352 /* actual system call traps */
1353 extern	int	__setcontext(const ucontext_t *);
1354 extern	int	__getcontext(ucontext_t *);
1355 extern	int	__clock_gettime(clockid_t, timespec_t *);
1356 extern	void	abstime_to_reltime(clockid_t, const timespec_t *, timespec_t *);
1357 extern	void	hrt2ts(hrtime_t, timespec_t *);
1358 
1359 extern	int	__sigaction(int, const struct sigaction *, struct sigaction *);
1360 extern	int	__lwp_sigmask(int, const sigset_t *, sigset_t *);
1361 extern	void	__sighndlr(int, siginfo_t *, ucontext_t *, void (*)());
1362 extern	caddr_t	__sighndlrend;
1363 #pragma unknown_control_flow(__sighndlr)
1364 extern	void	_siglongjmp(sigjmp_buf, int);
1365 
1366 extern	int	_pthread_setspecific(pthread_key_t, const void *);
1367 extern	void	*_pthread_getspecific(pthread_key_t);
1368 extern	void	_pthread_exit(void *);
1369 extern	int	_pthread_setcancelstate(int, int *);
1370 
1371 /* belongs in <pthread.h> */
1372 #define	PTHREAD_CREATE_DAEMON_NP	0x100	/* = THR_DAEMON */
1373 #define	PTHREAD_CREATE_NONDAEMON_NP	0
1374 extern	int	_pthread_attr_setdaemonstate_np(pthread_attr_t *, int);
1375 extern	int	_pthread_attr_getdaemonstate_np(const pthread_attr_t *, int *);
1376 
1377 extern	int	_mutex_init(mutex_t *, int, void *);
1378 extern	int	_mutex_destroy(mutex_t *);
1379 extern	int	_mutex_consistent(mutex_t *);
1380 extern	int	_mutex_lock(mutex_t *);
1381 extern	int	_mutex_trylock(mutex_t *);
1382 extern	int	_mutex_unlock(mutex_t *);
1383 extern	int	__mutex_init(mutex_t *, int, void *);
1384 extern	int	__mutex_destroy(mutex_t *);
1385 extern	int	__mutex_consistent(mutex_t *);
1386 extern	int	__mutex_lock(mutex_t *);
1387 extern	int	__mutex_trylock(mutex_t *);
1388 extern	int	__mutex_unlock(mutex_t *);
1389 extern	int	mutex_is_held(mutex_t *);
1390 extern	int	mutex_lock_internal(mutex_t *, timespec_t *, int);
1391 extern	int	mutex_unlock_internal(mutex_t *, int);
1392 
1393 extern	int	_cond_init(cond_t *, int, void *);
1394 extern	int	_cond_signal(cond_t *);
1395 extern	int	_cond_broadcast(cond_t *);
1396 extern	int	_cond_destroy(cond_t *);
1397 extern	int	cond_signal_internal(cond_t *);
1398 extern	int	cond_broadcast_internal(cond_t *);
1399 /* cancellation points: */
1400 extern	int	_cond_wait(cond_t *, mutex_t *);
1401 extern	int	_cond_timedwait(cond_t *, mutex_t *, const timespec_t *);
1402 extern	int	_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
1403 /* not cancellation points: */
1404 extern	int	__cond_wait(cond_t *, mutex_t *);
1405 extern	int	__cond_timedwait(cond_t *, mutex_t *, const timespec_t *);
1406 extern	int	__cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
1407 
1408 extern	int	__rwlock_init(rwlock_t *, int, void *);
1409 extern	int	rw_read_is_held(rwlock_t *);
1410 extern	int	rw_write_is_held(rwlock_t *);
1411 
1412 extern	void	_membar_enter(void);
1413 extern	void	_membar_exit(void);
1414 extern	void	_membar_producer(void);
1415 extern	void	_membar_consumer(void);
1416 
1417 extern	int	_thr_continue(thread_t);
1418 extern	int	_thr_create(void *, size_t, void *(*)(void *), void *, long,
1419 			thread_t *);
1420 extern	int	_thrp_create(void *, size_t, void *(*)(void *), void *, long,
1421 			thread_t *, size_t);
1422 extern	int	_thr_getspecific(thread_key_t, void **);
1423 extern	int	_thr_join(thread_t, thread_t *, void **);
1424 extern	int	_thr_keycreate(thread_key_t *, PFrV);
1425 extern	int	_thr_keycreate_once(thread_key_t *, PFrV);
1426 extern	int	_thr_key_delete(thread_key_t);
1427 extern	int	_thr_main(void);
1428 extern	thread_t _thr_self(void);
1429 extern	int	_thr_getconcurrency(void);
1430 extern	int	_thr_setconcurrency(int);
1431 extern	int	_thr_setprio(thread_t, int);
1432 extern	int	_thr_setspecific(thread_key_t, void *);
1433 extern	int	_thr_stksegment(stack_t *);
1434 extern	int	_thrp_suspend(thread_t, uchar_t);
1435 extern	int	_thrp_continue(thread_t, uchar_t);
1436 extern	int	_thr_sigsetmask(int, const sigset_t *, sigset_t *);
1437 
1438 extern	void	_thr_terminate(void *);
1439 extern	void	_thr_exit(void *);
1440 extern	void	_thrp_exit(void);
1441 
1442 extern	const pcclass_t *get_info_by_class(id_t);
1443 extern	const pcclass_t *get_info_by_policy(int);
1444 extern	void	_membar_producer(void);
1445 extern	void	_membar_consumer(void);
1446 extern	const thrattr_t *def_thrattr(void);
1447 extern	id_t	setparam(idtype_t, id_t, int, int);
1448 extern	id_t	setprio(idtype_t, id_t, int, int *);
1449 extern	id_t	getparam(idtype_t, id_t, int *, struct sched_param *);
1450 
1451 /*
1452  * System call wrappers (direct interfaces to the kernel)
1453  */
1454 extern	int	___lwp_mutex_register(mutex_t *);
1455 extern	int	___lwp_mutex_trylock(mutex_t *);
1456 extern	int	___lwp_mutex_timedlock(mutex_t *, timespec_t *);
1457 extern	int	___lwp_mutex_unlock(mutex_t *);
1458 extern	int	___lwp_mutex_wakeup(mutex_t *, int);
1459 extern	int	___lwp_cond_wait(cond_t *, mutex_t *, timespec_t *, int);
1460 extern	int	__lwp_cond_signal(lwp_cond_t *);
1461 extern	int	__lwp_cond_broadcast(lwp_cond_t *);
1462 extern	int	___lwp_sema_timedwait(lwp_sema_t *, timespec_t *, int);
1463 extern	int	__lwp_sema_trywait(lwp_sema_t *);
1464 extern	int	__lwp_sema_post(lwp_sema_t *);
1465 extern	int	__lwp_rwlock_rdlock(rwlock_t *, timespec_t *);
1466 extern	int	__lwp_rwlock_wrlock(rwlock_t *, timespec_t *);
1467 extern	int	__lwp_rwlock_tryrdlock(rwlock_t *);
1468 extern	int	__lwp_rwlock_trywrlock(rwlock_t *);
1469 extern	int	__lwp_rwlock_unlock(rwlock_t *);
1470 extern	int	__lwp_park(timespec_t *, lwpid_t);
1471 extern	int	__lwp_unpark(lwpid_t);
1472 extern	int	__lwp_unpark_all(lwpid_t *, int);
1473 #if defined(__x86)
1474 extern	int	___lwp_private(int, int, void *);
1475 #endif	/* __x86 */
1476 
1477 /*
1478  * inlines
1479  */
1480 extern	int		set_lock_byte(volatile uint8_t *);
1481 extern	uint32_t	atomic_swap_32(volatile uint32_t *, uint32_t);
1482 extern	uint32_t	atomic_cas_32(volatile uint32_t *, uint32_t, uint32_t);
1483 extern	void		atomic_inc_32(volatile uint32_t *);
1484 extern	void		atomic_dec_32(volatile uint32_t *);
1485 extern	void		atomic_and_32(volatile uint32_t *, uint32_t);
1486 extern	void		atomic_or_32(volatile uint32_t *, uint32_t);
1487 #if defined(__sparc)
1488 extern	ulong_t		caller(void);
1489 extern	ulong_t		getfp(void);
1490 #endif	/* __sparc */
1491 
1492 #include "thr_inlines.h"
1493 
1494 #endif	/* _THR_UBERDATA_H */
1495