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