xref: /titanic_52/usr/src/lib/libc/inc/thr_uberdata.h (revision 29493bd8e037cbaea9095b34172305abb589cb6b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _THR_UBERDATA_H
28 #define	_THR_UBERDATA_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include <signal.h>
38 #include <ucontext.h>
39 #include <thread.h>
40 #include <pthread.h>
41 #include <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;
512 	char		ul_save_state;	/* bind_guard() interface to ld.so.1 */
513 	int		ul_adaptive_spin;	/* thread_adaptive_spin */
514 	int		ul_queue_spin;		/* thread_queue_spin */
515 	volatile int	ul_critical;	/* non-zero == in a critical region */
516 	int		ul_sigdefer;	/* non-zero == defer signals */
517 	int		ul_vfork;	/* thread is the child of vfork() */
518 	int		ul_cancelable;	/* _cancelon()/_canceloff() */
519 	char		ul_cancel_pending;  /* pthread_cancel() was called */
520 	char		ul_cancel_disabled; /* PTHREAD_CANCEL_DISABLE */
521 	char		ul_cancel_async;    /* PTHREAD_CANCEL_ASYNCHRONOUS */
522 	char		ul_save_async;	/* saved copy of ul_cancel_async */
523 	char		ul_mutator;	/* lwp is a mutator (java interface) */
524 	char		ul_created;	/* created suspended */
525 	char		ul_replace;	/* replacement; must be free()d */
526 	uchar_t		ul_nocancel;	/* cancellation can't happen */
527 	int		ul_errno;	/* per-thread errno */
528 	int		*ul_errnop;	/* pointer to errno or self->ul_errno */
529 	__cleanup_t	*ul_clnup_hdr;	/* head of cleanup handlers list */
530 	uberflags_t *volatile ul_schedctl_called; /* ul_schedctl is set up */
531 	volatile sc_shared_t *volatile ul_schedctl;	/* schedctl data */
532 	int		ul_bindflags;	/* bind_guard() interface to ld.so.1 */
533 	uint_t		ul_libc_locks;	/* count of cancel_safe_mutex_lock()s */
534 	tsd_t		*ul_stsd;	/* slow TLS for keys >= TSD_NFAST */
535 	void		*ul_ftsd[TSD_NFAST]; /* fast TLS for keys < TSD_NFAST */
536 	td_evbuf_t	ul_td_evbuf;	/* event buffer */
537 	char		ul_td_events_enable;	/* event mechanism enabled */
538 	char		ul_sync_obj_reg;	/* tdb_sync_obj_register() */
539 	char		ul_qtype;	/* MX or CV */
540 	char		ul_cv_wake;	/* != 0: just wake up, don't requeue */
541 	int		ul_usropts;	/* flags given to thr_create() */
542 	void		*(*ul_startpc)(void *); /* start func (thr_create()) */
543 	void		*ul_startarg;	/* argument for start function */
544 	void		*ul_wchan;	/* synch object when sleeping */
545 	struct ulwp	*ul_link;	/* sleep queue link */
546 	queue_head_t	*ul_sleepq;	/* sleep queue thread is waiting on */
547 	mutex_t		*ul_cvmutex;	/* mutex dropped when waiting on a cv */
548 	mxchain_t	*ul_mxchain;	/* chain of owned ceiling mutexes */
549 	pri_t		ul_epri;	/* effective scheduling priority */
550 	pri_t		ul_emappedpri;	/* effective mapped priority */
551 	uint_t		ul_rdlockcnt;	/* # entries in ul_readlock array */
552 				/* 0 means there is but a single entry */
553 	union {				/* single entry or pointer to array */
554 		readlock_t	single;
555 		readlock_t	*array;
556 	} ul_readlock;
557 	uint_t		ul_heldlockcnt;	/* # entries in ul_heldlocks array */
558 				/* 0 means there is but a single entry */
559 	union {				/* single entry or pointer to array */
560 		mutex_t		*single;
561 		mutex_t		**array;
562 	} ul_heldlocks;
563 	/* PROBE_SUPPORT begin */
564 	void		*ul_tpdp;
565 	/* PROBE_SUPPORT end */
566 	ucontext_t	*ul_siglink;	/* pointer to previous context */
567 	uint_t		ul_spin_lock_spin;	/* spin lock statistics */
568 	uint_t		ul_spin_lock_spin2;
569 	uint_t		ul_spin_lock_sleep;
570 	uint_t		ul_spin_lock_wakeup;
571 		/* the following members *must* be last in the structure */
572 		/* they are discarded when ulwp is replaced on thr_exit() */
573 	sigset_t	ul_sigmask;	/* thread's current signal mask */
574 	sigset_t	ul_tmpmask;	/* signal mask for sigsuspend/pollsys */
575 	siginfo_t	ul_siginfo;	/* deferred siginfo */
576 	mutex_t		ul_spinlock;	/* used when suspending/continuing */
577 	fpuenv_t	ul_fpuenv;	/* floating point state */
578 	uintptr_t	ul_sp;		/* stack pointer when blocked */
579 	void		*ul_ex_unwind;	/* address of _ex_unwind() or -1 */
580 #if defined(sparc)
581 	void		*ul_unwind_ret;	/* used only by _ex_clnup_handler() */
582 #endif
583 } ulwp_t;
584 
585 #define	ul_cursig	ul_cp.s.cursig		/* deferred signal number */
586 #define	ul_pleasestop	ul_cp.s.pleasestop	/* lwp requested to stop */
587 #define	ul_curplease	ul_cp.curplease		/* for testing both at once */
588 
589 /*
590  * This is the size of a replacement ulwp, retained only for the benefit
591  * of thr_join().  The trailing members are unneeded for this purpose.
592  */
593 #define	REPLACEMENT_SIZE	((size_t)&((ulwp_t *)NULL)->ul_sigmask)
594 
595 /*
596  * Definitions for static initialization of signal sets,
597  * plus some sneaky optimizations in various places.
598  */
599 
600 #define	SIGMASK(sig)	((uint32_t)1 << (((sig) - 1) & (32 - 1)))
601 
602 #if (MAXSIG > 32 && MAXSIG <= 64)
603 #define	FILLSET0	0xffffffffu
604 #define	FILLSET1	((1u << (MAXSIG - 32)) - 1)
605 #else
606 #error "fix me: MAXSIG out of bounds"
607 #endif
608 
609 #define	CANTMASK0	(SIGMASK(SIGKILL) | SIGMASK(SIGSTOP))
610 #define	CANTMASK1	0
611 
612 #define	MASKSET0	(FILLSET0 & ~CANTMASK0)
613 #define	MASKSET1	(FILLSET1 & ~CANTMASK1)
614 
615 extern	const sigset_t maskset;		/* set of all maskable signals */
616 
617 extern	int	thread_adaptive_spin;
618 extern	uint_t	thread_max_spinners;
619 extern	int	thread_queue_spin;
620 extern	int	thread_queue_fifo;
621 extern	int	thread_queue_dump;
622 extern	int	thread_cond_wait_defer;
623 extern	int	thread_async_safe;
624 extern	int	thread_queue_verify;
625 
626 /*
627  * pthread_atfork() related data, used to store atfork handlers.
628  */
629 typedef struct atfork {
630 	struct atfork *forw;		/* forward pointer */
631 	struct atfork *back;		/* backward pointer */
632 	void (*prepare)(void);		/* pre-fork handler */
633 	void (*parent)(void);		/* post-fork parent handler */
634 	void (*child)(void);		/* post-fork child handler */
635 } atfork_t;
636 
637 /*
638  * Element in the table of registered process robust locks.
639  * We keep track of these to make sure that we only call
640  * ___lwp_mutex_register() once for each such lock.
641  */
642 typedef struct robust {
643 	struct robust	*robust_next;
644 	mutex_t		*robust_lock;
645 } robust_t;
646 
647 /*
648  * Parameters of the lock registration hash table.
649  */
650 #define	LOCKSHIFT	9			/* number of hashing bits */
651 #define	LOCKHASHSZ	(1 << LOCKSHIFT)	/* power of 2 (1<<9 == 512) */
652 #define	LOCK_HASH(addr)	(uint_t)			\
653 	((((uintptr_t)(addr) >> 3)			\
654 	^ ((uintptr_t)(addr) >> (LOCKSHIFT + 3)))	\
655 	& (LOCKHASHSZ - 1))
656 
657 /*
658  * Make our hot locks reside on private cache lines (64 bytes).
659  */
660 typedef struct {
661 	mutex_t	pad_lock;
662 	char	pad_pad[64 - sizeof (mutex_t)];
663 } pad_lock_t;
664 
665 /*
666  * Make our semi-hot locks reside on semi-private cache lines (32 bytes).
667  */
668 typedef struct {
669 	mutex_t	pad_lock;
670 	char	pad_pad[32 - sizeof (mutex_t)];
671 } pad32_lock_t;
672 
673 /*
674  * The threads hash table is used for fast lookup and locking of an active
675  * thread structure (ulwp_t) given a thread-id.  It is an N-element array of
676  * thr_hash_table_t structures, where N == 1 before the main thread creates
677  * the first additional thread and N == 1024 afterwards.  Each element of the
678  * table is 64 bytes in size and alignment to reduce cache conflicts.
679  */
680 typedef struct {
681 	mutex_t	hash_lock;	/* lock per bucket */
682 	cond_t	hash_cond;	/* convar per bucket */
683 	ulwp_t	*hash_bucket;	/* hash bucket points to the list of ulwps */
684 	char	hash_pad[64 -	/* pad out to 64 bytes */
685 		(sizeof (mutex_t) + sizeof (cond_t) + sizeof (ulwp_t *))];
686 } thr_hash_table_t;
687 
688 #ifdef _SYSCALL32
689 typedef struct {
690 	mutex_t	hash_lock;
691 	cond_t	hash_cond;
692 	caddr32_t hash_bucket;
693 	char	hash_pad[64 -
694 		(sizeof (mutex_t) + sizeof (cond_t) + sizeof (caddr32_t))];
695 } thr_hash_table32_t;
696 #endif	/* _SYSCALL32 */
697 
698 
699 /*
700  * siguaction members have 128-byte size and 64-byte alignment.
701  * We know that sizeof (struct sigaction) is 32 bytes for both
702  * _ILP32 and _LP64 and that sizeof (rwlock_t) is 64 bytes.
703  */
704 typedef struct {
705 	rwlock_t	sig_lock;
706 	struct sigaction sig_uaction;
707 	char	sig_pad[128 - sizeof (rwlock_t) - sizeof (struct sigaction)];
708 } siguaction_t;
709 
710 #ifdef _SYSCALL32
711 typedef struct {
712 	rwlock_t	sig_lock;
713 	struct sigaction32 sig_uaction;
714 	char	sig_pad[128 - sizeof (rwlock_t) - sizeof (struct sigaction32)];
715 } siguaction32_t;
716 #endif	/* _SYSCALL32 */
717 
718 
719 /*
720  * Bucket structures, used by lmalloc()/lfree().
721  * See port/threads/alloc.c for details.
722  * A bucket's size and alignment is 64 bytes.
723  */
724 typedef struct {
725 	mutex_t	bucket_lock;	/* protects the free list allocations */
726 	void	*free_list;	/* LIFO list of blocks to allocate/free */
727 	size_t	chunks;		/* number of 64K blocks mmap()ed last time */
728 	char	pad64[64 -	/* pad out to 64 bytes */
729 		(sizeof (mutex_t) + sizeof (void *) + sizeof (size_t))];
730 } bucket_t;
731 
732 #ifdef _SYSCALL32
733 typedef struct {
734 	mutex_t		bucket_lock;
735 	caddr32_t	free_list;
736 	size32_t	chunks;
737 	char	pad64[64 -	/* pad out to 64 bytes */
738 		(sizeof (mutex_t) + sizeof (caddr32_t) + sizeof (size32_t))];
739 } bucket32_t;
740 #endif	/* _SYSCALL32 */
741 
742 #define	NBUCKETS	10	/* sizes ranging from 64 to 32768 */
743 
744 
745 /*
746  * atexit() data structures.
747  * See port/gen/atexit.c for details.
748  */
749 typedef void (*_exithdlr_func_t) (void);
750 
751 typedef struct _exthdlr {
752 	struct _exthdlr 	*next;	/* next in handler list */
753 	_exithdlr_func_t	hdlr;	/* handler itself */
754 } _exthdlr_t;
755 
756 typedef struct {
757 	mutex_t		exitfns_lock;
758 	_exthdlr_t	*head;
759 	void		*exit_frame_monitor;
760 	char		exit_pad[64 -	/* pad out to 64 bytes */
761 		(sizeof (mutex_t) + sizeof (_exthdlr_t *) + sizeof (void *))];
762 } atexit_root_t;
763 
764 #ifdef _SYSCALL32
765 typedef struct {
766 	mutex_t		exitfns_lock;
767 	caddr32_t	head;
768 	caddr32_t	exit_frame_monitor;
769 	char		exit_pad[64 -	/* pad out to 64 bytes */
770 		(sizeof (mutex_t) + sizeof (caddr32_t) + sizeof (caddr32_t))];
771 } atexit_root32_t;
772 #endif	/* _SYSCALL32 */
773 
774 
775 /*
776  * This is data that is global to all link maps (uberdata, aka super-global).
777  */
778 typedef struct uberdata {
779 	pad_lock_t	_link_lock;
780 	pad32_lock_t	_fork_lock;
781 	pad32_lock_t	_atfork_lock;
782 	pad32_lock_t	_callout_lock;
783 	pad32_lock_t	_tdb_hash_lock;
784 	tdb_sync_stats_t tdb_hash_lock_stats;
785 	siguaction_t	siguaction[NSIG];
786 	bucket_t	bucket[NBUCKETS];
787 	atexit_root_t	atexit_root;
788 	tsd_metadata_t	tsd_metadata;
789 	tls_metadata_t	tls_metadata;
790 	/*
791 	 * Every object before this point has size and alignment of 64 bytes.
792 	 * Don't add any other type of data before this point.
793 	 */
794 	char	primary_map;	/* set when primary link map is initialized */
795 	char	bucket_init;	/* set when bucket[NBUCKETS] is initialized */
796 	char	pad[2];
797 	uberflags_t	uberflags;
798 	queue_head_t	*queue_head;
799 	thr_hash_table_t *thr_hash_table;
800 	uint_t		hash_size;	/* # of entries in thr_hash_table[] */
801 	uint_t		hash_mask;	/* hash_size - 1 */
802 	ulwp_t	*ulwp_one;	/* main thread */
803 	ulwp_t	*all_lwps;	/* circular ul_forw/ul_back list of live lwps */
804 	ulwp_t	*all_zombies;	/* circular ul_forw/ul_back list of zombies */
805 	int	nthreads;	/* total number of live threads/lwps */
806 	int	nzombies;	/* total number of zombie threads */
807 	int	ndaemons;	/* total number of THR_DAEMON threads/lwps */
808 	pid_t	pid;		/* the current process's pid */
809 	void	(*sigacthandler)(int, siginfo_t *, void *);
810 	ulwp_t	*lwp_stacks;
811 	ulwp_t	*lwp_laststack;
812 	int	nfreestack;
813 	int	thread_stack_cache;
814 	ulwp_t	*ulwp_freelist;
815 	ulwp_t	*ulwp_lastfree;
816 	ulwp_t	*ulwp_replace_free;
817 	ulwp_t	*ulwp_replace_last;
818 	atfork_t	*atforklist;	/* circular Q for fork handlers */
819 	robust_t	**robustlocks;	/* table of registered robust locks */
820 	struct uberdata **tdb_bootstrap;
821 	tdb_t	tdb;		/* thread debug interfaces (for libc_db) */
822 } uberdata_t;
823 
824 #define	link_lock	_link_lock.pad_lock
825 #define	fork_lock	_fork_lock.pad_lock
826 #define	atfork_lock	_atfork_lock.pad_lock
827 #define	callout_lock	_callout_lock.pad_lock
828 #define	tdb_hash_lock	_tdb_hash_lock.pad_lock
829 
830 #pragma align 64(__uberdata)
831 extern	uberdata_t	__uberdata;
832 extern	uberdata_t	**__tdb_bootstrap;	/* known to libc_db and mdb */
833 extern	int		primary_link_map;
834 
835 #define	ulwp_mutex(ulwp, udp)	\
836 	(&(udp)->thr_hash_table[(ulwp)->ul_ix].hash_lock)
837 #define	ulwp_condvar(ulwp, udp)	\
838 	(&(udp)->thr_hash_table[(ulwp)->ul_ix].hash_cond)
839 
840 /*
841  * Grab and release the hash table lock for the specified lwp.
842  */
843 #define	ulwp_lock(ulwp, udp)	lmutex_lock(ulwp_mutex(ulwp, udp))
844 #define	ulwp_unlock(ulwp, udp)	lmutex_unlock(ulwp_mutex(ulwp, udp))
845 
846 #ifdef _SYSCALL32	/* needed by libc_db */
847 
848 typedef struct ulwp32 {
849 #if defined(__sparc)
850 	uint32_t	ul_dinstr;	/* scratch space for dtrace */
851 	uint32_t	ul_padsparc0[15];
852 	uint32_t	ul_dsave;	/* dtrace: save %g1, %g0, %sp */
853 	uint32_t	ul_drestore;	/* dtrace: restore %g0, %g0, %g0 */
854 	uint32_t	ul_dftret;	/* dtrace: return probe fasttrap */
855 	uint32_t	ul_dreturn;	/* dtrace: return %o0 */
856 #endif
857 	caddr32_t	ul_self;	/* pointer to self */
858 #if defined(__x86)
859 	uint8_t		ul_dinstr[40];	/* scratch space for dtrace */
860 #endif
861 	caddr32_t	ul_uberdata;	/* uber (super-global) data */
862 	tls32_t		ul_tls;		/* dynamic thread-local storage base */
863 	caddr32_t	ul_forw;	/* forw, back all_lwps list, */
864 	caddr32_t	ul_back;	/* protected by link_lock */
865 	caddr32_t	ul_next;	/* list to keep track of stacks */
866 	caddr32_t	ul_hash;	/* hash chain linked list */
867 	caddr32_t	ul_rval;	/* return value from thr_exit() */
868 	caddr32_t	ul_stk;		/* mapping base of the stack */
869 	size32_t	ul_mapsiz;	/* mapping size of the stack */
870 	size32_t	ul_guardsize;	/* normally _lpagesize */
871 	caddr32_t	ul_stktop;	/* broken thr_stksegment() interface */
872 	size32_t	ul_stksiz;	/* broken thr_stksegment() interface */
873 	stack32_t	ul_ustack;	/* current stack boundaries */
874 	int		ul_ix;		/* hash index */
875 	lwpid_t		ul_lwpid;	/* thread id, aka the lwp id */
876 	pri_t		ul_pri;		/* priority known to the library */
877 	pri_t		ul_mappedpri;	/* priority known to the application */
878 	char		ul_policy;	/* scheduling policy */
879 	char		ul_pri_mapped;	/* != 0 means ul_mappedpri is valid */
880 	union {
881 		struct {
882 			char	cursig;	/* deferred signal number */
883 			char	pleasestop; /* lwp requested to stop itself */
884 		} s;
885 		short	curplease;	/* for testing both at once */
886 	} ul_cp;
887 	char		ul_stop;	/* reason for stopping */
888 	char		ul_signalled;	/* this lwp was cond_signal()d */
889 	char		ul_dead;	/* this lwp has called thr_exit */
890 	char		ul_unwind;	/* posix: unwind C++ stack */
891 	char		ul_detached;	/* THR_DETACHED at thread_create() */
892 					/* or pthread_detach() was called */
893 	char		ul_writer;	/* sleeping in rw_wrlock() */
894 	char		ul_stopping;	/* set by curthread: stopping self */
895 	char		ul_cancel_prologue;	/* for _cancel_prologue() */
896 	short		ul_preempt;	/* no_preempt()/preempt() */
897 	short		ul_savpreempt;	/* pre-existing preempt value */
898 	char		ul_sigsuspend;	/* thread is in sigsuspend/pollsys */
899 	char		ul_main;	/* thread is the main thread */
900 	char		ul_fork;	/* thread is performing a fork */
901 	char		ul_primarymap;	/* primary link-map is initialized */
902 	/* per-thread copies of the corresponding global variables */
903 	uint8_t		ul_max_spinners;	/* thread_max_spinners */
904 	char		ul_door_noreserve;	/* thread_door_noreserve */
905 	char		ul_queue_fifo;		/* thread_queue_fifo */
906 	char		ul_cond_wait_defer;	/* thread_cond_wait_defer */
907 	char		ul_error_detection;	/* thread_error_detection */
908 	char		ul_async_safe;		/* thread_async_safe */
909 	char		ul_pad1;
910 	char		ul_save_state;	/* bind_guard() interface to ld.so.1 */
911 	int		ul_adaptive_spin;	/* thread_adaptive_spin */
912 	int		ul_queue_spin;		/* thread_queue_spin */
913 	int		ul_critical;	/* non-zero == in a critical region */
914 	int		ul_sigdefer;	/* non-zero == defer signals */
915 	int		ul_vfork;	/* thread is the child of vfork() */
916 	int		ul_cancelable;	/* _cancelon()/_canceloff() */
917 	char		ul_cancel_pending;  /* pthread_cancel() was called */
918 	char		ul_cancel_disabled; /* PTHREAD_CANCEL_DISABLE */
919 	char		ul_cancel_async;    /* PTHREAD_CANCEL_ASYNCHRONOUS */
920 	char		ul_save_async;	/* saved copy of ul_cancel_async */
921 	char		ul_mutator;	/* lwp is a mutator (java interface) */
922 	char		ul_created;	/* created suspended */
923 	char		ul_replace;	/* replacement; must be free()d */
924 	uchar_t		ul_nocancel;	/* cancellation can't happen */
925 	int		ul_errno;	/* per-thread errno */
926 	caddr32_t	ul_errnop;	/* pointer to errno or self->ul_errno */
927 	caddr32_t	ul_clnup_hdr;	/* head of cleanup handlers list */
928 	caddr32_t	ul_schedctl_called; /* ul_schedctl is set up */
929 	caddr32_t	ul_schedctl;	/* schedctl data */
930 	int		ul_bindflags;	/* bind_guard() interface to ld.so.1 */
931 	uint_t		ul_libc_locks;	/* count of cancel_safe_mutex_lock()s */
932 	caddr32_t	ul_stsd;	/* slow TLS for keys >= TSD_NFAST */
933 	caddr32_t	ul_ftsd[TSD_NFAST]; /* fast TLS for keys < TSD_NFAST */
934 	td_evbuf32_t	ul_td_evbuf;	/* event buffer */
935 	char		ul_td_events_enable;	/* event mechanism enabled */
936 	char		ul_sync_obj_reg;	/* tdb_sync_obj_register() */
937 	char		ul_qtype;	/* MX or CV */
938 	char		ul_cv_wake;	/* != 0: just wake up, don't requeue */
939 	int		ul_usropts;	/* flags given to thr_create() */
940 	caddr32_t	ul_startpc;	/* start func (thr_create()) */
941 	caddr32_t	ul_startarg;	/* argument for start function */
942 	caddr32_t	ul_wchan;	/* synch object when sleeping */
943 	caddr32_t	ul_link;	/* sleep queue link */
944 	caddr32_t	ul_sleepq;	/* sleep queue thread is waiting on */
945 	caddr32_t	ul_cvmutex;	/* mutex dropped when waiting on a cv */
946 	caddr32_t	ul_mxchain;	/* chain of owned ceiling mutexes */
947 	pri_t		ul_epri;	/* effective scheduling priority */
948 	pri_t		ul_emappedpri;	/* effective mapped priority */
949 	uint_t		ul_rdlockcnt;	/* # entries in ul_readlock array */
950 				/* 0 means there is but a single entry */
951 	union {				/* single entry or pointer to array */
952 		readlock32_t	single;
953 		caddr32_t	array;
954 	} ul_readlock;
955 	uint_t		ul_heldlockcnt;	/* # entries in ul_heldlocks array */
956 				/* 0 means there is but a single entry */
957 	union {				/* single entry or pointer to array */
958 		caddr32_t	single;
959 		caddr32_t	array;
960 	} ul_heldlocks;
961 	/* PROBE_SUPPORT begin */
962 	caddr32_t	ul_tpdp;
963 	/* PROBE_SUPPORT end */
964 	caddr32_t	ul_siglink;	/* pointer to previous context */
965 	uint_t		ul_spin_lock_spin;	/* spin lock statistics */
966 	uint_t		ul_spin_lock_spin2;
967 	uint_t		ul_spin_lock_sleep;
968 	uint_t		ul_spin_lock_wakeup;
969 		/* the following members *must* be last in the structure */
970 		/* they are discarded when ulwp is replaced on thr_exit() */
971 	sigset32_t	ul_sigmask;	/* thread's current signal mask */
972 	sigset32_t	ul_tmpmask;	/* signal mask for sigsuspend/pollsys */
973 	siginfo32_t	ul_siginfo;	/* deferred siginfo */
974 	mutex_t		ul_spinlock;	/* used when suspending/continuing */
975 	fpuenv32_t	ul_fpuenv;	/* floating point state */
976 	caddr32_t	ul_sp;		/* stack pointer when blocked */
977 #if defined(sparc)
978 	caddr32_t	ul_unwind_ret;	/* used only by _ex_clnup_handler() */
979 #endif
980 } ulwp32_t;
981 
982 #define	REPLACEMENT_SIZE32	((size_t)&((ulwp32_t *)NULL)->ul_sigmask)
983 
984 typedef struct uberdata32 {
985 	pad_lock_t	_link_lock;
986 	pad32_lock_t	_fork_lock;
987 	pad32_lock_t	_atfork_lock;
988 	pad32_lock_t	_callout_lock;
989 	pad32_lock_t	_tdb_hash_lock;
990 	tdb_sync_stats_t tdb_hash_lock_stats;
991 	siguaction32_t	siguaction[NSIG];
992 	bucket32_t	bucket[NBUCKETS];
993 	atexit_root32_t	atexit_root;
994 	tsd_metadata32_t tsd_metadata;
995 	tls_metadata32_t tls_metadata;
996 	char		primary_map;
997 	char		bucket_init;
998 	char		pad[2];
999 	uberflags_t	uberflags;
1000 	caddr32_t	queue_head;
1001 	caddr32_t	thr_hash_table;
1002 	uint_t		hash_size;
1003 	uint_t		hash_mask;
1004 	caddr32_t	ulwp_one;
1005 	caddr32_t	all_lwps;
1006 	caddr32_t	all_zombies;
1007 	int		nthreads;
1008 	int		nzombies;
1009 	int		ndaemons;
1010 	int		pid;
1011 	caddr32_t	sigacthandler;
1012 	caddr32_t	lwp_stacks;
1013 	caddr32_t	lwp_laststack;
1014 	int		nfreestack;
1015 	int		thread_stack_cache;
1016 	caddr32_t	ulwp_freelist;
1017 	caddr32_t	ulwp_lastfree;
1018 	caddr32_t	ulwp_replace_free;
1019 	caddr32_t	ulwp_replace_last;
1020 	caddr32_t	atforklist;
1021 	caddr32_t	robustlocks;
1022 	caddr32_t	tdb_bootstrap;
1023 	tdb32_t		tdb;
1024 } uberdata32_t;
1025 
1026 #endif	/* _SYSCALL32 */
1027 
1028 /* ul_stop values */
1029 #define	TSTP_REGULAR	0x01	/* Stopped by thr_suspend() */
1030 #define	TSTP_MUTATOR	0x08	/* stopped by thr_suspend_*mutator*() */
1031 #define	TSTP_FORK	0x20	/* stopped by suspend_fork() */
1032 
1033 /*
1034  * Implementation-specific attribute types for pthread_mutexattr_init() etc.
1035  */
1036 
1037 typedef	struct	_cvattr {
1038 	int	pshared;
1039 	clockid_t clockid;
1040 } cvattr_t;
1041 
1042 typedef	struct	_mattr {
1043 	int	pshared;
1044 	int	protocol;
1045 	int	prioceiling;
1046 	int	type;
1047 	int	robustness;
1048 } mattr_t;
1049 
1050 typedef	struct	_thrattr {
1051 	size_t	stksize;
1052 	void	*stkaddr;
1053 	int	detachstate;
1054 	int	daemonstate;
1055 	int	scope;
1056 	int	prio;
1057 	int	policy;
1058 	int	inherit;
1059 	size_t	guardsize;
1060 } thrattr_t;
1061 
1062 typedef	struct	_rwlattr {
1063 	int	pshared;
1064 } rwlattr_t;
1065 
1066 /* _curthread() is inline for speed */
1067 extern	ulwp_t		*_curthread(void);
1068 #define	curthread	(_curthread())
1069 
1070 /* this version (also inline) can be tested for NULL */
1071 extern	ulwp_t		*__curthread(void);
1072 
1073 /* get the current stack pointer (also inline) */
1074 extern	greg_t		stkptr(void);
1075 
1076 /*
1077  * Suppress __attribute__((...)) if we are not compiling with gcc
1078  */
1079 #if !defined(__GNUC__)
1080 #define	__attribute__(string)
1081 #endif
1082 
1083 /*
1084  * Implementation functions.  Not visible outside of the library itself.
1085  */
1086 extern	int	__nanosleep(const timespec_t *, timespec_t *);
1087 extern	void	getgregs(ulwp_t *, gregset_t);
1088 extern	void	setgregs(ulwp_t *, gregset_t);
1089 extern	void	thr_panic(const char *);
1090 #pragma rarely_called(thr_panic)
1091 extern	ulwp_t	*find_lwp(thread_t);
1092 extern	int	real_priority(ulwp_t *);
1093 extern	void	finish_init(void);
1094 extern	void	queue_alloc(void);
1095 extern	void	tsd_exit(void);
1096 extern	void	tsd_free(ulwp_t *);
1097 extern	void	tls_setup(void);
1098 extern	void	tls_exit(void);
1099 extern	void	tls_free(ulwp_t *);
1100 extern	void	rwl_free(ulwp_t *);
1101 extern	void	heldlock_exit(void);
1102 extern	void	heldlock_free(ulwp_t *);
1103 extern	void	sigacthandler(int, siginfo_t *, void *);
1104 extern	void	signal_init(void);
1105 extern	int	sigequalset(const sigset_t *, const sigset_t *);
1106 extern	void	mutex_setup(void);
1107 extern	void	take_deferred_signal(int);
1108 extern	int	setup_context(ucontext_t *, void *(*func)(ulwp_t *),
1109 			ulwp_t *ulwp, caddr_t stk, size_t stksize);
1110 extern	volatile sc_shared_t *setup_schedctl(void);
1111 extern	void	*lmalloc(size_t);
1112 extern	void	lfree(void *, size_t);
1113 extern	void	*libc_malloc(size_t);
1114 extern	void	*libc_realloc(void *, size_t);
1115 extern	void	libc_free(void *);
1116 extern	char	*libc_strdup(const char *);
1117 extern	void	ultos(uint64_t, int, char *);
1118 extern	void	lock_error(const mutex_t *, const char *, void *, const char *);
1119 extern	void	rwlock_error(const rwlock_t *, const char *, const char *);
1120 extern	void	thread_error(const char *);
1121 extern	void	grab_assert_lock(void);
1122 extern	void	dump_queue_statistics(void);
1123 extern	void	collect_queue_statistics(void);
1124 extern	void	record_spin_locks(ulwp_t *);
1125 extern	void	remember_lock(mutex_t *);
1126 extern	void	forget_lock(mutex_t *);
1127 extern	void	register_lock(mutex_t *);
1128 extern	void	unregister_locks(void);
1129 #if defined(__sparc)
1130 extern	void	_flush_windows(void);
1131 #else
1132 #define	_flush_windows()
1133 #endif
1134 extern	void	set_curthread(void *);
1135 
1136 /*
1137  * Utility function used when waking up many threads (more than MAXLWPS)
1138  * all at once.  See mutex_wakeup_all(), cond_broadcast(), and rw_unlock().
1139  */
1140 #define	MAXLWPS	128	/* max remembered lwpids before overflow */
1141 #define	NEWLWPS	2048	/* max remembered lwpids at first overflow */
1142 extern	lwpid_t	*alloc_lwpids(lwpid_t *, int *, int *);
1143 
1144 /* enter a critical section */
1145 #define	enter_critical(self)	(self->ul_critical++)
1146 
1147 /* exit a critical section, take deferred actions if necessary */
1148 extern	void	do_exit_critical(void);
1149 #define	exit_critical(self)					\
1150 	(void) (self->ul_critical--,				\
1151 	    ((self->ul_curplease && self->ul_critical == 0)?	\
1152 	    (do_exit_critical(), 0) : 0))
1153 
1154 /*
1155  * Like enter_critical()/exit_critical() but just for deferring signals.
1156  * Unlike enter_critical()/exit_critical(), ul_sigdefer may be set while
1157  * calling application functions like constructors and destructors.
1158  * Care must be taken if the application function attempts to set
1159  * the signal mask while a deferred signal is present; the setting
1160  * of the signal mask must also be deferred.
1161  */
1162 #define	sigoff(self)	(self->ul_sigdefer++)
1163 extern	void	sigon(ulwp_t *);
1164 
1165 /* these are exported functions */
1166 extern	void	_sigoff(void);
1167 extern	void	_sigon(void);
1168 
1169 #define	sigorset(s1, s2)				\
1170 	(((s1)->__sigbits[0] |= (s2)->__sigbits[0]),	\
1171 	((s1)->__sigbits[1] |= (s2)->__sigbits[1]),	\
1172 	((s1)->__sigbits[2] |= (s2)->__sigbits[2]),	\
1173 	((s1)->__sigbits[3] |= (s2)->__sigbits[3]))
1174 
1175 #define	sigandset(s1, s2)				\
1176 	(((s1)->__sigbits[0] &= (s2)->__sigbits[0]),	\
1177 	((s1)->__sigbits[1] &= (s2)->__sigbits[1]),	\
1178 	((s1)->__sigbits[2] &= (s2)->__sigbits[2]),	\
1179 	((s1)->__sigbits[3] &= (s2)->__sigbits[3]))
1180 
1181 #define	sigdiffset(s1, s2)				\
1182 	(((s1)->__sigbits[0] &= ~(s2)->__sigbits[0]),	\
1183 	((s1)->__sigbits[1] &= ~(s2)->__sigbits[1]),	\
1184 	((s1)->__sigbits[2] &= ~(s2)->__sigbits[2]),	\
1185 	((s1)->__sigbits[3] &= ~(s2)->__sigbits[3]))
1186 
1187 #define	delete_reserved_signals(s)			\
1188 	(((s)->__sigbits[0] &= MASKSET0),		\
1189 	((s)->__sigbits[1] &= (MASKSET1 & ~SIGMASK(SIGCANCEL))),\
1190 	((s)->__sigbits[2] = 0),			\
1191 	((s)->__sigbits[3] = 0))
1192 
1193 extern	void	block_all_signals(ulwp_t *self);
1194 
1195 /*
1196  * When restoring the signal mask after having previously called
1197  * block_all_signals(), if we have a deferred signal present then
1198  * do nothing other than ASSERT() that we are in a critical region.
1199  * The signal mask will be set when we emerge from the critical region
1200  * and call take_deferred_signal().  There is no race condition here
1201  * because the kernel currently has all signals blocked for this thread.
1202  */
1203 #define	restore_signals(self)						\
1204 	((void) ((self)->ul_cursig?					\
1205 	(ASSERT((self)->ul_critical + (self)->ul_sigdefer != 0), 0) :	\
1206 	__lwp_sigmask(SIG_SETMASK, &(self)->ul_sigmask, NULL)))
1207 
1208 extern	void	set_cancel_pending_flag(ulwp_t *, int);
1209 extern	void	set_cancel_eintr_flag(ulwp_t *);
1210 extern	void	set_parking_flag(ulwp_t *, int);
1211 extern	int	cancel_active(void);
1212 
1213 extern	void	*_thr_setup(ulwp_t *);
1214 extern	void	_fpinherit(ulwp_t *);
1215 extern	void	_lwp_start(void);
1216 extern	void	_lwp_terminate(void);
1217 extern	void	lmutex_lock(mutex_t *);
1218 extern	void	lmutex_unlock(mutex_t *);
1219 extern	void	lrw_rdlock(rwlock_t *);
1220 extern	void	lrw_wrlock(rwlock_t *);
1221 extern	void	lrw_unlock(rwlock_t *);
1222 extern	void	sig_mutex_lock(mutex_t *);
1223 extern	void	sig_mutex_unlock(mutex_t *);
1224 extern	int	sig_mutex_trylock(mutex_t *);
1225 extern	int	sig_cond_wait(cond_t *, mutex_t *);
1226 extern	int	sig_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
1227 extern	void	cancel_safe_mutex_lock(mutex_t *);
1228 extern	void	cancel_safe_mutex_unlock(mutex_t *);
1229 extern	int	cancel_safe_mutex_trylock(mutex_t *);
1230 extern	void	_prefork_handler(void);
1231 extern	void	_postfork_parent_handler(void);
1232 extern	void	_postfork_child_handler(void);
1233 extern	void	postfork1_child(void);
1234 extern	void	postfork1_child_aio(void);
1235 extern	void	postfork1_child_sigev_aio(void);
1236 extern	void	postfork1_child_sigev_mq(void);
1237 extern	void	postfork1_child_sigev_timer(void);
1238 extern	void	postfork1_child_tpool(void);
1239 extern	void	fork_lock_enter(void);
1240 extern	void	fork_lock_exit(void);
1241 extern	void	suspend_fork(void);
1242 extern	void	continue_fork(int);
1243 extern	void	do_sigcancel(void);
1244 extern	void	setup_cancelsig(int);
1245 extern	void	init_sigev_thread(void);
1246 extern	void	init_aio(void);
1247 extern	void	_cancelon(void);
1248 extern	void	_canceloff(void);
1249 extern	void	_canceloff_nocancel(void);
1250 extern	void	_cancel_prologue(void);
1251 extern	void	_cancel_epilogue(void);
1252 extern	void	no_preempt(ulwp_t *);
1253 extern	void	preempt(ulwp_t *);
1254 extern	void	_thrp_unwind(void *);
1255 
1256 /*
1257  * Prototypes for the strong versions of the interface functions
1258  */
1259 extern	pid_t	__forkx(int);
1260 extern	pid_t	__forkallx(int);
1261 extern	pid_t	_private_getpid(void);
1262 extern	uid_t	_private_geteuid(void);
1263 extern	int	_kill(pid_t, int);
1264 extern	int	_private_open(const char *, int, ...);
1265 extern	int	_private_close(int);
1266 extern	ssize_t	__read(int, void *, size_t);
1267 extern	ssize_t	__write(int, const void *, size_t);
1268 extern	void	*_memcpy(void *, const void *, size_t);
1269 extern	void	*_memset(void *, int, size_t);
1270 extern	int	_memcmp(const void *, const void *, size_t);
1271 extern	void	*_private_memcpy(void *, const void *, size_t);
1272 extern	void	*_private_memset(void *, int, size_t);
1273 extern	int	_private_sigfillset(sigset_t *);
1274 extern	int	_private_sigemptyset(sigset_t *);
1275 extern	int	_private_sigaddset(sigset_t *, int);
1276 extern	int	_private_sigdelset(sigset_t *, int);
1277 extern	int	_private_sigismember(sigset_t *, int);
1278 extern	void	*_private_mmap(void *, size_t, int, int, int, off_t);
1279 extern	int	_private_mprotect(void *, size_t, int);
1280 extern	int	_private_munmap(void *, size_t);
1281 extern	int	_private_getrlimit(int, struct rlimit *);
1282 extern	int	__lwp_continue(lwpid_t);
1283 extern	int	__lwp_create(ucontext_t *, uint_t, lwpid_t *);
1284 extern	int	__lwp_kill(lwpid_t, int);
1285 extern	lwpid_t	__lwp_self(void);
1286 extern	int	___lwp_suspend(lwpid_t);
1287 extern	void	lwp_yield(void);
1288 extern	int	lwp_wait(lwpid_t, lwpid_t *);
1289 extern	int	__lwp_wait(lwpid_t, lwpid_t *);
1290 extern	int	__lwp_detach(lwpid_t);
1291 extern	sc_shared_t *__schedctl(void);
1292 
1293 extern	int	_private_setcontext(const ucontext_t *);
1294 extern	int	_private_getcontext(ucontext_t *);
1295 #pragma unknown_control_flow(_private_getcontext)
1296 /* actual system call traps */
1297 extern	int	__setcontext_syscall(const ucontext_t *);
1298 extern	int	__getcontext_syscall(ucontext_t *);
1299 extern	int	_private_setustack(stack_t *);
1300 extern	int	__clock_gettime(clockid_t, timespec_t *);
1301 extern	void	abstime_to_reltime(clockid_t, const timespec_t *, timespec_t *);
1302 extern	void	hrt2ts(hrtime_t, timespec_t *);
1303 
1304 extern	int	__sigaction(int, const struct sigaction *, struct sigaction *);
1305 extern	int	__lwp_sigmask(int, const sigset_t *, sigset_t *);
1306 extern	void	__sighndlr(int, siginfo_t *, ucontext_t *, void (*)());
1307 extern	caddr_t	__sighndlrend;
1308 #pragma unknown_control_flow(__sighndlr)
1309 extern	void	_siglongjmp(sigjmp_buf, int);
1310 
1311 extern	int	_pthread_setspecific(pthread_key_t, const void *);
1312 extern	void	*_pthread_getspecific(pthread_key_t);
1313 extern	void	_pthread_exit(void *);
1314 extern	int	_pthread_setcancelstate(int, int *);
1315 extern	void	_private_testcancel(void);
1316 
1317 /* belongs in <pthread.h> */
1318 #define	PTHREAD_CREATE_DAEMON_NP	0x100	/* = THR_DAEMON */
1319 #define	PTHREAD_CREATE_NONDAEMON_NP	0
1320 extern	int	_pthread_attr_setdaemonstate_np(pthread_attr_t *, int);
1321 extern	int	_pthread_attr_getdaemonstate_np(const pthread_attr_t *, int *);
1322 
1323 /* these are private to the library */
1324 extern	int	_private_mutex_init(mutex_t *, int, void *);
1325 extern	int	_private_mutex_destroy(mutex_t *);
1326 extern	int	_private_mutex_lock(mutex_t *);
1327 extern	int	_private_mutex_trylock(mutex_t *);
1328 extern	int	_private_mutex_unlock(mutex_t *);
1329 
1330 extern	int	_mutex_init(mutex_t *, int, void *);
1331 extern	int	_mutex_destroy(mutex_t *);
1332 extern	int	_mutex_consistent(mutex_t *);
1333 extern	int	_mutex_lock(mutex_t *);
1334 extern	int	_mutex_trylock(mutex_t *);
1335 extern	int	_mutex_unlock(mutex_t *);
1336 extern	int	__mutex_init(mutex_t *, int, void *);
1337 extern	int	__mutex_destroy(mutex_t *);
1338 extern	int	__mutex_consistent(mutex_t *);
1339 extern	int	__mutex_lock(mutex_t *);
1340 extern	int	__mutex_trylock(mutex_t *);
1341 extern	int	__mutex_unlock(mutex_t *);
1342 extern	int	mutex_is_held(mutex_t *);
1343 
1344 extern	int	_cond_init(cond_t *, int, void *);
1345 extern	int	_cond_signal(cond_t *);
1346 extern	int	_cond_broadcast(cond_t *);
1347 extern	int	_cond_destroy(cond_t *);
1348 extern	int	cond_signal_internal(cond_t *);
1349 extern	int	cond_broadcast_internal(cond_t *);
1350 /* cancellation points: */
1351 extern	int	_cond_wait(cond_t *, mutex_t *);
1352 extern	int	_cond_timedwait(cond_t *, mutex_t *, const timespec_t *);
1353 extern	int	_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
1354 /* not cancellation points: */
1355 extern	int	__cond_wait(cond_t *, mutex_t *);
1356 extern	int	__cond_timedwait(cond_t *, mutex_t *, const timespec_t *);
1357 extern	int	__cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
1358 
1359 extern	int	__rwlock_init(rwlock_t *, int, void *);
1360 extern	int	rw_read_is_held(rwlock_t *);
1361 extern	int	rw_write_is_held(rwlock_t *);
1362 
1363 extern	void	_membar_enter(void);
1364 extern	void	_membar_exit(void);
1365 extern	void	_membar_producer(void);
1366 extern	void	_membar_consumer(void);
1367 
1368 extern	int	_thr_continue(thread_t);
1369 extern	int	_thr_create(void *, size_t, void *(*)(void *), void *, long,
1370 			thread_t *);
1371 extern	int	_thrp_create(void *, size_t, void *(*)(void *), void *, long,
1372 			thread_t *, pri_t, int, size_t);
1373 extern	int	_thr_getprio(thread_t, int *);
1374 extern	int	_thr_getspecific(thread_key_t, void **);
1375 extern	int	_thr_join(thread_t, thread_t *, void **);
1376 extern	int	_thr_keycreate(thread_key_t *, PFrV);
1377 extern	int	_thr_keycreate_once(thread_key_t *, PFrV);
1378 extern	int	_thr_key_delete(thread_key_t);
1379 extern	int	_thr_main(void);
1380 extern	thread_t _thr_self(void);
1381 extern	int	_thr_getconcurrency(void);
1382 extern	int	_thr_setconcurrency(int);
1383 extern	int	_thr_setprio(thread_t, int);
1384 extern	int	_thr_setspecific(thread_key_t, void *);
1385 extern	int	_thr_stksegment(stack_t *);
1386 extern	int	_thrp_suspend(thread_t, uchar_t);
1387 extern	int	_thrp_continue(thread_t, uchar_t);
1388 extern	int	_thr_sigsetmask(int, const sigset_t *, sigset_t *);
1389 
1390 extern	void	_thr_terminate(void *);
1391 extern	void	_thr_exit(void *);
1392 extern	void	_thrp_exit(void);
1393 
1394 extern	const thrattr_t *def_thrattr(void);
1395 extern	int	_thread_setschedparam_main(pthread_t, int,
1396 			const struct sched_param *, int);
1397 extern	int	_validate_rt_prio(int, int);
1398 extern	int	_thrp_setlwpprio(lwpid_t, int, int);
1399 extern	pri_t	map_rtpri_to_gp(pri_t);
1400 extern	int	get_info_by_policy(int);
1401 
1402 /*
1403  * System call wrappers (direct interfaces to the kernel)
1404  */
1405 extern	int	___lwp_mutex_register(mutex_t *);
1406 extern	int	___lwp_mutex_trylock(mutex_t *);
1407 extern	int	___lwp_mutex_timedlock(mutex_t *, timespec_t *);
1408 extern	int	___lwp_mutex_unlock(mutex_t *);
1409 extern	int	___lwp_mutex_wakeup(mutex_t *, int);
1410 extern	int	___lwp_cond_wait(cond_t *, mutex_t *, timespec_t *, int);
1411 extern	int	__lwp_cond_signal(lwp_cond_t *);
1412 extern	int	__lwp_cond_broadcast(lwp_cond_t *);
1413 extern	int	___lwp_sema_timedwait(lwp_sema_t *, timespec_t *, int);
1414 extern	int	__lwp_sema_trywait(lwp_sema_t *);
1415 extern	int	__lwp_sema_post(lwp_sema_t *);
1416 extern	int	__lwp_rwlock_rdlock(rwlock_t *, timespec_t *);
1417 extern	int	__lwp_rwlock_wrlock(rwlock_t *, timespec_t *);
1418 extern	int	__lwp_rwlock_tryrdlock(rwlock_t *);
1419 extern	int	__lwp_rwlock_trywrlock(rwlock_t *);
1420 extern	int	__lwp_rwlock_unlock(rwlock_t *);
1421 extern	int	__lwp_park(timespec_t *, lwpid_t);
1422 extern	int	__lwp_unpark(lwpid_t);
1423 extern	int	__lwp_unpark_all(lwpid_t *, int);
1424 #if defined(__x86)
1425 extern	int	___lwp_private(int, int, void *);
1426 #endif	/* __x86 */
1427 
1428 extern	int	_private_lwp_mutex_lock(mutex_t *);
1429 extern	int	_private_lwp_mutex_unlock(mutex_t *);
1430 
1431 /*
1432  * inlines
1433  */
1434 extern	int		set_lock_byte(volatile uint8_t *);
1435 extern	uint32_t	atomic_swap_32(volatile uint32_t *, uint32_t);
1436 extern	uint32_t	atomic_cas_32(volatile uint32_t *, uint32_t, uint32_t);
1437 extern	void		atomic_inc_32(volatile uint32_t *);
1438 extern	void		atomic_dec_32(volatile uint32_t *);
1439 extern	void		atomic_and_32(volatile uint32_t *, uint32_t);
1440 extern	void		atomic_or_32(volatile uint32_t *, uint32_t);
1441 #if defined(__sparc)
1442 extern	ulong_t		caller(void);
1443 extern	ulong_t		getfp(void);
1444 #endif	/* __sparc */
1445 
1446 #include "thr_inlines.h"
1447 
1448 #endif	/* _THR_UBERDATA_H */
1449