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