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