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