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