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