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