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