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