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