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