1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 #include "lint.h" 31 #include "thr_uberdata.h" 32 #include <procfs.h> 33 #include <sys/uio.h> 34 #include <ctype.h> 35 36 #undef errno 37 extern int errno; 38 39 int __threaded = 0; /* zero until first thr_create() */ 40 41 /* 42 * thr_concurrency and pthread_concurrency are not used by the library. 43 * They exist solely to hold and return the values set by calls to 44 * thr_setconcurrency() and pthread_setconcurrency(). 45 * Because thr_concurrency is affected by the THR_NEW_LWP flag 46 * to thr_create(), thr_concurrency is protected by link_lock. 47 */ 48 static int thr_concurrency = 1; 49 static int pthread_concurrency; 50 51 size_t _lpagesize; 52 53 #define HASHTBLSZ 1024 /* must be a power of two */ 54 #define TIDHASH(tid, udp) (tid & (udp)->hash_mask) 55 56 /* initial allocation, just enough for one lwp */ 57 #pragma align 64(init_hash_table) 58 thr_hash_table_t init_hash_table[1] = { 59 { DEFAULTMUTEX, DEFAULTCV, NULL }, 60 }; 61 62 extern const Lc_interface rtld_funcs[]; 63 64 /* 65 * The weak version is known to libc_db and mdb. 66 */ 67 #pragma weak _uberdata = __uberdata 68 uberdata_t __uberdata = { 69 { DEFAULTMUTEX, DEFAULTCV }, /* link_lock */ 70 { DEFAULTMUTEX, DEFAULTCV }, /* fork_lock */ 71 { DEFAULTMUTEX, DEFAULTCV }, /* tdb_hash_lock */ 72 { 0, }, /* tdb_hash_lock_stats */ 73 { { 0 }, }, /* siguaction[NSIG] */ 74 {{ DEFAULTMUTEX, NULL, 0 }, /* bucket[NBUCKETS] */ 75 { DEFAULTMUTEX, NULL, 0 }, 76 { DEFAULTMUTEX, NULL, 0 }, 77 { DEFAULTMUTEX, NULL, 0 }, 78 { DEFAULTMUTEX, NULL, 0 }, 79 { DEFAULTMUTEX, NULL, 0 }, 80 { DEFAULTMUTEX, NULL, 0 }, 81 { DEFAULTMUTEX, NULL, 0 }, 82 { DEFAULTMUTEX, NULL, 0 }, 83 { DEFAULTMUTEX, NULL, 0 }}, 84 { RECURSIVEMUTEX, NULL, NULL }, /* atexit_root */ 85 { DEFAULTMUTEX, 0, 0, NULL }, /* tsd_metadata */ 86 { DEFAULTMUTEX, {0, 0}, {0, 0} }, /* tls_metadata */ 87 0, /* primary_map */ 88 0, /* bucket_init */ 89 0, /* pad[0] */ 90 0, /* pad[1] */ 91 { 0 }, /* uberflags */ 92 NULL, /* queue_head */ 93 init_hash_table, /* thr_hash_table */ 94 1, /* hash_size: size of the hash table */ 95 0, /* hash_mask: hash_size - 1 */ 96 NULL, /* ulwp_one */ 97 NULL, /* all_lwps */ 98 NULL, /* all_zombies */ 99 0, /* nthreads */ 100 0, /* nzombies */ 101 0, /* ndaemons */ 102 0, /* pid */ 103 sigacthandler, /* sigacthandler */ 104 NULL, /* lwp_stacks */ 105 NULL, /* lwp_laststack */ 106 0, /* nfreestack */ 107 10, /* thread_stack_cache */ 108 NULL, /* ulwp_freelist */ 109 NULL, /* ulwp_lastfree */ 110 NULL, /* ulwp_replace_free */ 111 NULL, /* ulwp_replace_last */ 112 NULL, /* atforklist */ 113 NULL, /* __tdb_bootstrap */ 114 { /* tdb */ 115 NULL, /* tdb_sync_addr_hash */ 116 0, /* tdb_register_count */ 117 0, /* tdb_hash_alloc_failed */ 118 NULL, /* tdb_sync_addr_free */ 119 NULL, /* tdb_sync_addr_last */ 120 0, /* tdb_sync_alloc */ 121 { 0, 0 }, /* tdb_ev_global_mask */ 122 tdb_events, /* tdb_events array */ 123 }, 124 }; 125 126 /* 127 * The weak version is known to libc_db and mdb. 128 */ 129 #pragma weak _tdb_bootstrap = __tdb_bootstrap 130 uberdata_t **__tdb_bootstrap = NULL; 131 132 int thread_queue_fifo = 4; 133 int thread_queue_dump = 0; 134 int thread_cond_wait_defer = 0; 135 int thread_error_detection = 0; 136 int thread_async_safe = 0; 137 int thread_stack_cache = 10; 138 139 int thread_door_noreserve = 0; 140 141 static ulwp_t *ulwp_alloc(void); 142 static void ulwp_free(ulwp_t *); 143 144 /* 145 * Insert the lwp into the hash table. 146 */ 147 void 148 hash_in_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp) 149 { 150 ulwp->ul_hash = udp->thr_hash_table[ix].hash_bucket; 151 udp->thr_hash_table[ix].hash_bucket = ulwp; 152 ulwp->ul_ix = ix; 153 } 154 155 void 156 hash_in(ulwp_t *ulwp, uberdata_t *udp) 157 { 158 int ix = TIDHASH(ulwp->ul_lwpid, udp); 159 mutex_t *mp = &udp->thr_hash_table[ix].hash_lock; 160 161 lmutex_lock(mp); 162 hash_in_unlocked(ulwp, ix, udp); 163 lmutex_unlock(mp); 164 } 165 166 /* 167 * Delete the lwp from the hash table. 168 */ 169 void 170 hash_out_unlocked(ulwp_t *ulwp, int ix, uberdata_t *udp) 171 { 172 ulwp_t **ulwpp; 173 174 for (ulwpp = &udp->thr_hash_table[ix].hash_bucket; 175 ulwp != *ulwpp; 176 ulwpp = &(*ulwpp)->ul_hash) 177 ; 178 *ulwpp = ulwp->ul_hash; 179 ulwp->ul_hash = NULL; 180 ulwp->ul_ix = -1; 181 } 182 183 void 184 hash_out(ulwp_t *ulwp, uberdata_t *udp) 185 { 186 int ix; 187 188 if ((ix = ulwp->ul_ix) >= 0) { 189 mutex_t *mp = &udp->thr_hash_table[ix].hash_lock; 190 191 lmutex_lock(mp); 192 hash_out_unlocked(ulwp, ix, udp); 193 lmutex_unlock(mp); 194 } 195 } 196 197 static void 198 ulwp_clean(ulwp_t *ulwp) 199 { 200 ulwp->ul_self = NULL; 201 ulwp->ul_rval = NULL; 202 ulwp->ul_lwpid = 0; 203 ulwp->ul_pri = 0; 204 ulwp->ul_mappedpri = 0; 205 ulwp->ul_policy = 0; 206 ulwp->ul_pri_mapped = 0; 207 ulwp->ul_mutator = 0; 208 ulwp->ul_pleasestop = 0; 209 ulwp->ul_stop = 0; 210 ulwp->ul_dead = 0; 211 ulwp->ul_unwind = 0; 212 ulwp->ul_detached = 0; 213 ulwp->ul_stopping = 0; 214 ulwp->ul_sp = 0; 215 ulwp->ul_critical = 0; 216 ulwp->ul_cancelable = 0; 217 ulwp->ul_preempt = 0; 218 ulwp->ul_sigsuspend = 0; 219 ulwp->ul_cancel_pending = 0; 220 ulwp->ul_cancel_disabled = 0; 221 ulwp->ul_cancel_async = 0; 222 ulwp->ul_save_async = 0; 223 ulwp->ul_cursig = 0; 224 ulwp->ul_created = 0; 225 ulwp->ul_replace = 0; 226 ulwp->ul_schedctl_called = NULL; 227 ulwp->ul_errno = 0; 228 ulwp->ul_errnop = NULL; 229 ulwp->ul_clnup_hdr = NULL; 230 ulwp->ul_schedctl = NULL; 231 ulwp->ul_bindflags = 0; 232 (void) _private_memset(&ulwp->ul_td_evbuf, 0, 233 sizeof (ulwp->ul_td_evbuf)); 234 ulwp->ul_td_events_enable = 0; 235 ulwp->ul_qtype = 0; 236 ulwp->ul_usropts = 0; 237 ulwp->ul_startpc = NULL; 238 ulwp->ul_startarg = NULL; 239 ulwp->ul_wchan = NULL; 240 ulwp->ul_link = NULL; 241 ulwp->ul_sleepq = NULL; 242 ulwp->ul_mxchain = NULL; 243 ulwp->ul_epri = 0; 244 ulwp->ul_emappedpri = 0; 245 /* PROBE_SUPPORT begin */ 246 ulwp->ul_tpdp = NULL; 247 /* PROBE_SUPPORT end */ 248 ulwp->ul_siglink = NULL; 249 (void) _private_memset(ulwp->ul_ftsd, 0, 250 sizeof (void *) * TSD_NFAST); 251 ulwp->ul_stsd = NULL; 252 (void) _private_memset(&ulwp->ul_spinlock, 0, 253 sizeof (ulwp->ul_spinlock)); 254 ulwp->ul_spin_lock_spin = 0; 255 ulwp->ul_spin_lock_spin2 = 0; 256 ulwp->ul_spin_lock_sleep = 0; 257 ulwp->ul_spin_lock_wakeup = 0; 258 ulwp->ul_ex_unwind = NULL; 259 } 260 261 static int stackprot; 262 263 /* 264 * Answer the question, "Is the lwp in question really dead?" 265 * We must inquire of the operating system to be really sure 266 * because the lwp may have called lwp_exit() but it has not 267 * yet completed the exit. 268 */ 269 static int 270 dead_and_buried(ulwp_t *ulwp) 271 { 272 if (ulwp->ul_lwpid == (lwpid_t)(-1)) 273 return (1); 274 if (ulwp->ul_dead && ulwp->ul_detached && 275 __lwp_kill(ulwp->ul_lwpid, 0) == ESRCH) { 276 ulwp->ul_lwpid = (lwpid_t)(-1); 277 return (1); 278 } 279 return (0); 280 } 281 282 /* 283 * Attempt to keep the stack cache within the specified cache limit. 284 */ 285 static void 286 trim_stack_cache(int cache_limit) 287 { 288 ulwp_t *self = curthread; 289 uberdata_t *udp = self->ul_uberdata; 290 ulwp_t *prev = NULL; 291 ulwp_t **ulwpp = &udp->lwp_stacks; 292 ulwp_t *ulwp; 293 294 ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, self)); 295 296 while (udp->nfreestack > cache_limit && (ulwp = *ulwpp) != NULL) { 297 if (dead_and_buried(ulwp)) { 298 *ulwpp = ulwp->ul_next; 299 if (ulwp == udp->lwp_laststack) 300 udp->lwp_laststack = prev; 301 hash_out(ulwp, udp); 302 udp->nfreestack--; 303 (void) _private_munmap(ulwp->ul_stk, ulwp->ul_mapsiz); 304 /* 305 * Now put the free ulwp on the ulwp freelist. 306 */ 307 ulwp->ul_mapsiz = 0; 308 ulwp->ul_next = NULL; 309 if (udp->ulwp_freelist == NULL) 310 udp->ulwp_freelist = udp->ulwp_lastfree = ulwp; 311 else { 312 udp->ulwp_lastfree->ul_next = ulwp; 313 udp->ulwp_lastfree = ulwp; 314 } 315 } else { 316 prev = ulwp; 317 ulwpp = &ulwp->ul_next; 318 } 319 } 320 } 321 322 /* 323 * Find an unused stack of the requested size 324 * or create a new stack of the requested size. 325 * Return a pointer to the ulwp_t structure referring to the stack, or NULL. 326 * thr_exit() stores 1 in the ul_dead member. 327 * thr_join() stores -1 in the ul_lwpid member. 328 */ 329 ulwp_t * 330 find_stack(size_t stksize, size_t guardsize) 331 { 332 uberdata_t *udp = curthread->ul_uberdata; 333 size_t mapsize; 334 ulwp_t *prev; 335 ulwp_t *ulwp; 336 ulwp_t **ulwpp; 337 void *stk; 338 339 /* 340 * The stack is allocated PROT_READ|PROT_WRITE|PROT_EXEC 341 * unless overridden by the system's configuration. 342 */ 343 if (stackprot == 0) { /* do this once */ 344 long lprot = _sysconf(_SC_STACK_PROT); 345 if (lprot <= 0) 346 lprot = (PROT_READ|PROT_WRITE|PROT_EXEC); 347 stackprot = (int)lprot; 348 } 349 if (_lpagesize == 0) 350 _lpagesize = _sysconf(_SC_PAGESIZE); 351 /* 352 * One megabyte stacks by default, but subtract off 353 * two pages for the system-created red zones. 354 * Round up a non-zero stack size to a pagesize multiple. 355 */ 356 if (stksize == 0) 357 stksize = DEFAULTSTACK - 2 * _lpagesize; 358 else 359 stksize = ((stksize + _lpagesize - 1) & -_lpagesize); 360 361 /* 362 * Round up the mapping size to a multiple of pagesize. 363 * Note: mmap() provides at least one page of red zone 364 * so we deduct that from the value of guardsize. 365 */ 366 if (guardsize != 0) 367 guardsize = ((guardsize + _lpagesize - 1) & -_lpagesize) - 368 _lpagesize; 369 mapsize = stksize + guardsize; 370 371 lmutex_lock(&udp->link_lock); 372 for (prev = NULL, ulwpp = &udp->lwp_stacks; 373 (ulwp = *ulwpp) != NULL; 374 prev = ulwp, ulwpp = &ulwp->ul_next) { 375 if (ulwp->ul_mapsiz == mapsize && 376 ulwp->ul_guardsize == guardsize && 377 dead_and_buried(ulwp)) { 378 /* 379 * The previous lwp is gone; reuse the stack. 380 * Remove the ulwp from the stack list. 381 */ 382 *ulwpp = ulwp->ul_next; 383 ulwp->ul_next = NULL; 384 if (ulwp == udp->lwp_laststack) 385 udp->lwp_laststack = prev; 386 hash_out(ulwp, udp); 387 udp->nfreestack--; 388 lmutex_unlock(&udp->link_lock); 389 ulwp_clean(ulwp); 390 return (ulwp); 391 } 392 } 393 394 /* 395 * None of the cached stacks matched our mapping size. 396 * Reduce the stack cache to get rid of possibly 397 * very old stacks that will never be reused. 398 */ 399 if (udp->nfreestack > udp->thread_stack_cache) 400 trim_stack_cache(udp->thread_stack_cache); 401 else if (udp->nfreestack > 0) 402 trim_stack_cache(udp->nfreestack - 1); 403 lmutex_unlock(&udp->link_lock); 404 405 /* 406 * Create a new stack. 407 */ 408 if ((stk = _private_mmap(NULL, mapsize, stackprot, 409 MAP_PRIVATE|MAP_NORESERVE|MAP_ANON, -1, (off_t)0)) != MAP_FAILED) { 410 /* 411 * We have allocated our stack. Now allocate the ulwp. 412 */ 413 ulwp = ulwp_alloc(); 414 if (ulwp == NULL) 415 (void) _private_munmap(stk, mapsize); 416 else { 417 ulwp->ul_stk = stk; 418 ulwp->ul_mapsiz = mapsize; 419 ulwp->ul_guardsize = guardsize; 420 ulwp->ul_stktop = (uintptr_t)stk + mapsize; 421 ulwp->ul_stksiz = stksize; 422 ulwp->ul_ix = -1; 423 if (guardsize) /* protect the extra red zone */ 424 (void) _private_mprotect(stk, 425 guardsize, PROT_NONE); 426 } 427 } 428 return (ulwp); 429 } 430 431 /* 432 * Get a ulwp_t structure from the free list or allocate a new one. 433 * Such ulwp_t's do not have a stack allocated by the library. 434 */ 435 static ulwp_t * 436 ulwp_alloc(void) 437 { 438 ulwp_t *self = curthread; 439 uberdata_t *udp = self->ul_uberdata; 440 size_t tls_size; 441 ulwp_t *prev; 442 ulwp_t *ulwp; 443 ulwp_t **ulwpp; 444 caddr_t data; 445 446 lmutex_lock(&udp->link_lock); 447 for (prev = NULL, ulwpp = &udp->ulwp_freelist; 448 (ulwp = *ulwpp) != NULL; 449 prev = ulwp, ulwpp = &ulwp->ul_next) { 450 if (dead_and_buried(ulwp)) { 451 *ulwpp = ulwp->ul_next; 452 ulwp->ul_next = NULL; 453 if (ulwp == udp->ulwp_lastfree) 454 udp->ulwp_lastfree = prev; 455 hash_out(ulwp, udp); 456 lmutex_unlock(&udp->link_lock); 457 ulwp_clean(ulwp); 458 return (ulwp); 459 } 460 } 461 lmutex_unlock(&udp->link_lock); 462 463 tls_size = roundup64(udp->tls_metadata.static_tls.tls_size); 464 data = lmalloc(sizeof (*ulwp) + tls_size); 465 if (data != NULL) { 466 /* LINTED pointer cast may result in improper alignment */ 467 ulwp = (ulwp_t *)(data + tls_size); 468 } 469 return (ulwp); 470 } 471 472 /* 473 * Free a ulwp structure. 474 * If there is an associated stack, put it on the stack list and 475 * munmap() previously freed stacks up to the residual cache limit. 476 * Else put it on the ulwp free list and never call lfree() on it. 477 */ 478 static void 479 ulwp_free(ulwp_t *ulwp) 480 { 481 uberdata_t *udp = curthread->ul_uberdata; 482 483 ASSERT(udp->nthreads <= 1 || MUTEX_OWNED(&udp->link_lock, curthread)); 484 ulwp->ul_next = NULL; 485 if (ulwp == udp->ulwp_one) /* don't reuse the primoridal stack */ 486 /*EMPTY*/; 487 else if (ulwp->ul_mapsiz != 0) { 488 if (udp->lwp_stacks == NULL) 489 udp->lwp_stacks = udp->lwp_laststack = ulwp; 490 else { 491 udp->lwp_laststack->ul_next = ulwp; 492 udp->lwp_laststack = ulwp; 493 } 494 if (++udp->nfreestack > udp->thread_stack_cache) 495 trim_stack_cache(udp->thread_stack_cache); 496 } else { 497 if (udp->ulwp_freelist == NULL) 498 udp->ulwp_freelist = udp->ulwp_lastfree = ulwp; 499 else { 500 udp->ulwp_lastfree->ul_next = ulwp; 501 udp->ulwp_lastfree = ulwp; 502 } 503 } 504 } 505 506 /* 507 * Find a named lwp and return a pointer to its hash list location. 508 * On success, returns with the hash lock held. 509 */ 510 ulwp_t ** 511 find_lwpp(thread_t tid) 512 { 513 uberdata_t *udp = curthread->ul_uberdata; 514 int ix = TIDHASH(tid, udp); 515 mutex_t *mp = &udp->thr_hash_table[ix].hash_lock; 516 ulwp_t *ulwp; 517 ulwp_t **ulwpp; 518 519 if (tid == 0) 520 return (NULL); 521 522 lmutex_lock(mp); 523 for (ulwpp = &udp->thr_hash_table[ix].hash_bucket; 524 (ulwp = *ulwpp) != NULL; 525 ulwpp = &ulwp->ul_hash) { 526 if (ulwp->ul_lwpid == tid) 527 return (ulwpp); 528 } 529 lmutex_unlock(mp); 530 return (NULL); 531 } 532 533 /* 534 * Wake up all lwps waiting on this lwp for some reason. 535 */ 536 void 537 ulwp_broadcast(ulwp_t *ulwp) 538 { 539 ulwp_t *self = curthread; 540 uberdata_t *udp = self->ul_uberdata; 541 542 ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self)); 543 (void) cond_broadcast_internal(ulwp_condvar(ulwp, udp)); 544 } 545 546 /* 547 * Find a named lwp and return a pointer to it. 548 * Returns with the hash lock held. 549 */ 550 ulwp_t * 551 find_lwp(thread_t tid) 552 { 553 ulwp_t *self = curthread; 554 uberdata_t *udp = self->ul_uberdata; 555 ulwp_t *ulwp = NULL; 556 ulwp_t **ulwpp; 557 558 if (self->ul_lwpid == tid) { 559 ulwp = self; 560 ulwp_lock(ulwp, udp); 561 } else if ((ulwpp = find_lwpp(tid)) != NULL) { 562 ulwp = *ulwpp; 563 } 564 565 if (ulwp && ulwp->ul_dead) { 566 ulwp_unlock(ulwp, udp); 567 ulwp = NULL; 568 } 569 570 return (ulwp); 571 } 572 573 int 574 _thrp_create(void *stk, size_t stksize, void *(*func)(void *), void *arg, 575 long flags, thread_t *new_thread, pri_t priority, int policy, 576 size_t guardsize) 577 { 578 ulwp_t *self = curthread; 579 uberdata_t *udp = self->ul_uberdata; 580 ucontext_t uc; 581 uint_t lwp_flags; 582 thread_t tid; 583 int error = 0; 584 ulwp_t *ulwp; 585 586 /* 587 * Enforce the restriction of not creating any threads 588 * until the primary link map has been initialized. 589 * Also, disallow thread creation to a child of vfork(). 590 */ 591 if (!self->ul_primarymap || self->ul_vfork) 592 return (ENOTSUP); 593 594 if (udp->hash_size == 1) 595 finish_init(); 596 597 if (((stk || stksize) && stksize < MINSTACK) || 598 priority < THREAD_MIN_PRIORITY || priority > THREAD_MAX_PRIORITY) 599 return (EINVAL); 600 601 if (stk == NULL) { 602 if ((ulwp = find_stack(stksize, guardsize)) == NULL) 603 return (ENOMEM); 604 stksize = ulwp->ul_mapsiz - ulwp->ul_guardsize; 605 } else { 606 /* initialize the private stack */ 607 if ((ulwp = ulwp_alloc()) == NULL) 608 return (ENOMEM); 609 ulwp->ul_stk = stk; 610 ulwp->ul_stktop = (uintptr_t)stk + stksize; 611 ulwp->ul_stksiz = stksize; 612 ulwp->ul_ix = -1; 613 } 614 ulwp->ul_errnop = &ulwp->ul_errno; 615 616 lwp_flags = LWP_SUSPENDED; 617 if (flags & (THR_DETACHED|THR_DAEMON)) { 618 flags |= THR_DETACHED; 619 lwp_flags |= LWP_DETACHED; 620 } 621 if (flags & THR_DAEMON) 622 lwp_flags |= LWP_DAEMON; 623 624 /* creating a thread: enforce mt-correctness in _mutex_lock() */ 625 self->ul_async_safe = 1; 626 627 /* per-thread copies of global variables, for speed */ 628 ulwp->ul_queue_fifo = self->ul_queue_fifo; 629 ulwp->ul_cond_wait_defer = self->ul_cond_wait_defer; 630 ulwp->ul_error_detection = self->ul_error_detection; 631 ulwp->ul_async_safe = self->ul_async_safe; 632 ulwp->ul_max_spinners = self->ul_max_spinners; 633 ulwp->ul_adaptive_spin = self->ul_adaptive_spin; 634 ulwp->ul_release_spin = self->ul_release_spin; 635 ulwp->ul_queue_spin = self->ul_queue_spin; 636 ulwp->ul_door_noreserve = self->ul_door_noreserve; 637 638 ulwp->ul_primarymap = self->ul_primarymap; 639 ulwp->ul_self = ulwp; 640 ulwp->ul_uberdata = udp; 641 642 /* debugger support */ 643 ulwp->ul_usropts = flags; 644 645 #ifdef __sparc 646 /* 647 * We cache several instructions in the thread structure for use 648 * by the fasttrap DTrace provider. When changing this, read the 649 * comment in fasttrap.h for the all the other places that must 650 * be changed. 651 */ 652 ulwp->ul_dsave = 0x9de04000; /* save %g1, %g0, %sp */ 653 ulwp->ul_drestore = 0x81e80000; /* restore %g0, %g0, %g0 */ 654 ulwp->ul_dftret = 0x91d0203a; /* ta 0x3a */ 655 ulwp->ul_dreturn = 0x81ca0000; /* return %o0 */ 656 #endif 657 658 ulwp->ul_startpc = func; 659 ulwp->ul_startarg = arg; 660 _fpinherit(ulwp); 661 /* 662 * Defer signals on the new thread until its TLS constructors 663 * have been called. _thr_setup() will call sigon() after 664 * it has called tls_setup(). 665 */ 666 ulwp->ul_sigdefer = 1; 667 668 if (setup_context(&uc, _thr_setup, ulwp, 669 (caddr_t)ulwp->ul_stk + ulwp->ul_guardsize, stksize) != 0) 670 error = EAGAIN; 671 672 /* 673 * Call enter_critical() to avoid being suspended until we 674 * have linked the new thread into the proper lists. 675 * This is necessary because forkall() and fork1() must 676 * suspend all threads and they must see a complete list. 677 */ 678 enter_critical(self); 679 uc.uc_sigmask = ulwp->ul_sigmask = self->ul_sigmask; 680 if (error != 0 || 681 (error = __lwp_create(&uc, lwp_flags, &tid)) != 0) { 682 exit_critical(self); 683 ulwp->ul_lwpid = (lwpid_t)(-1); 684 ulwp->ul_dead = 1; 685 ulwp->ul_detached = 1; 686 lmutex_lock(&udp->link_lock); 687 ulwp_free(ulwp); 688 lmutex_unlock(&udp->link_lock); 689 return (error); 690 } 691 self->ul_nocancel = 0; /* cancellation is now possible */ 692 ulwp->ul_nocancel = 0; 693 udp->uberflags.uf_mt = 1; 694 if (new_thread) 695 *new_thread = tid; 696 if (flags & THR_DETACHED) 697 ulwp->ul_detached = 1; 698 ulwp->ul_lwpid = tid; 699 ulwp->ul_stop = TSTP_REGULAR; 700 ulwp->ul_created = 1; 701 ulwp->ul_policy = policy; 702 ulwp->ul_pri = priority; 703 704 lmutex_lock(&udp->link_lock); 705 ulwp->ul_forw = udp->all_lwps; 706 ulwp->ul_back = udp->all_lwps->ul_back; 707 ulwp->ul_back->ul_forw = ulwp; 708 ulwp->ul_forw->ul_back = ulwp; 709 hash_in(ulwp, udp); 710 udp->nthreads++; 711 if (flags & THR_DAEMON) 712 udp->ndaemons++; 713 if (flags & THR_NEW_LWP) 714 thr_concurrency++; 715 __threaded = 1; /* inform stdio */ 716 lmutex_unlock(&udp->link_lock); 717 718 if (__td_event_report(self, TD_CREATE, udp)) { 719 self->ul_td_evbuf.eventnum = TD_CREATE; 720 self->ul_td_evbuf.eventdata = (void *)(uintptr_t)tid; 721 tdb_event(TD_CREATE, udp); 722 } 723 if (!(flags & THR_SUSPENDED)) { 724 ulwp->ul_created = 0; 725 (void) _thrp_continue(tid, TSTP_REGULAR); 726 } 727 728 exit_critical(self); 729 return (0); 730 } 731 732 #pragma weak thr_create = _thr_create 733 int 734 _thr_create(void *stk, size_t stksize, void *(*func)(void *), void *arg, 735 long flags, thread_t *new_thread) 736 { 737 return (_thrp_create(stk, stksize, func, arg, flags, new_thread, 738 curthread->ul_pri, curthread->ul_policy, 0)); 739 } 740 741 /* 742 * A special cancellation cleanup hook for DCE. 743 * cleanuphndlr, when it is not NULL, will contain a callback 744 * function to be called before a thread is terminated in 745 * _thr_exit() as a result of being cancelled. 746 */ 747 static void (*cleanuphndlr)(void) = NULL; 748 749 /* 750 * _pthread_setcleanupinit: sets the cleanup hook. 751 */ 752 int 753 _pthread_setcleanupinit(void (*func)(void)) 754 { 755 cleanuphndlr = func; 756 return (0); 757 } 758 759 void 760 _thrp_exit() 761 { 762 ulwp_t *self = curthread; 763 uberdata_t *udp = self->ul_uberdata; 764 ulwp_t *replace = NULL; 765 766 if (__td_event_report(self, TD_DEATH, udp)) { 767 self->ul_td_evbuf.eventnum = TD_DEATH; 768 tdb_event(TD_DEATH, udp); 769 } 770 771 ASSERT(self->ul_sigdefer != 0); 772 773 lmutex_lock(&udp->link_lock); 774 udp->nthreads--; 775 if (self->ul_usropts & THR_NEW_LWP) 776 thr_concurrency--; 777 if (self->ul_usropts & THR_DAEMON) 778 udp->ndaemons--; 779 else if (udp->nthreads == udp->ndaemons) { 780 /* 781 * We are the last non-daemon thread exiting. 782 * Exit the process. We retain our TSD and TLS so 783 * that atexit() application functions can use them. 784 */ 785 lmutex_unlock(&udp->link_lock); 786 exit(0); 787 thr_panic("_thrp_exit(): exit(0) returned"); 788 } 789 lmutex_unlock(&udp->link_lock); 790 791 tsd_exit(); /* deallocate thread-specific data */ 792 tls_exit(); /* deallocate thread-local storage */ 793 794 /* block all signals to finish exiting */ 795 block_all_signals(self); 796 /* also prevent ourself from being suspended */ 797 enter_critical(self); 798 rwl_free(self); 799 lmutex_lock(&udp->link_lock); 800 ulwp_free(self); 801 (void) ulwp_lock(self, udp); 802 803 if (self->ul_mapsiz && !self->ul_detached) { 804 /* 805 * We want to free the stack for reuse but must keep 806 * the ulwp_t struct for the benefit of thr_join(). 807 * For this purpose we allocate a replacement ulwp_t. 808 */ 809 if ((replace = udp->ulwp_replace_free) == NULL) 810 replace = lmalloc(REPLACEMENT_SIZE); 811 else if ((udp->ulwp_replace_free = replace->ul_next) == NULL) 812 udp->ulwp_replace_last = NULL; 813 } 814 815 if (udp->all_lwps == self) 816 udp->all_lwps = self->ul_forw; 817 if (udp->all_lwps == self) 818 udp->all_lwps = NULL; 819 else { 820 self->ul_forw->ul_back = self->ul_back; 821 self->ul_back->ul_forw = self->ul_forw; 822 } 823 self->ul_forw = self->ul_back = NULL; 824 /* collect queue lock statistics before marking ourself dead */ 825 record_spin_locks(self); 826 self->ul_dead = 1; 827 self->ul_pleasestop = 0; 828 if (replace != NULL) { 829 int ix = self->ul_ix; /* the hash index */ 830 (void) _private_memcpy(replace, self, REPLACEMENT_SIZE); 831 replace->ul_self = replace; 832 replace->ul_gs = 0; /* clone does not carry %gs */ 833 replace->ul_next = NULL; /* clone not on stack list */ 834 replace->ul_mapsiz = 0; /* allows clone to be freed */ 835 replace->ul_replace = 1; /* requires clone to be freed */ 836 hash_out_unlocked(self, ix, udp); 837 hash_in_unlocked(replace, ix, udp); 838 ASSERT(!(self->ul_detached)); 839 self->ul_detached = 1; /* this frees the stack */ 840 self->ul_schedctl = NULL; 841 self->ul_schedctl_called = &udp->uberflags; 842 set_curthread(self = replace); 843 /* 844 * Having just changed the address of curthread, we 845 * must reset the ownership of the locks we hold so 846 * that assertions will not fire when we release them. 847 */ 848 udp->link_lock.mutex_owner = (uintptr_t)self; 849 ulwp_mutex(self, udp)->mutex_owner = (uintptr_t)self; 850 /* 851 * NOTE: 852 * On i386, %gs still references the original, not the 853 * replacement, ulwp structure. Fetching the replacement 854 * curthread pointer via %gs:0 works correctly since the 855 * original ulwp structure will not be reallocated until 856 * this lwp has completed its lwp_exit() system call (see 857 * dead_and_buried()), but from here on out, we must make 858 * no references to %gs:<offset> other than %gs:0. 859 */ 860 } 861 /* 862 * Put non-detached terminated threads in the all_zombies list. 863 */ 864 if (!self->ul_detached) { 865 udp->nzombies++; 866 if (udp->all_zombies == NULL) { 867 ASSERT(udp->nzombies == 1); 868 udp->all_zombies = self->ul_forw = self->ul_back = self; 869 } else { 870 self->ul_forw = udp->all_zombies; 871 self->ul_back = udp->all_zombies->ul_back; 872 self->ul_back->ul_forw = self; 873 self->ul_forw->ul_back = self; 874 } 875 } 876 /* 877 * Notify everyone waiting for this thread. 878 */ 879 ulwp_broadcast(self); 880 (void) ulwp_unlock(self, udp); 881 /* 882 * Prevent any more references to the schedctl data. 883 * We are exiting and continue_fork() may not find us. 884 * Do this just before dropping link_lock, since fork 885 * serializes on link_lock. 886 */ 887 self->ul_schedctl = NULL; 888 self->ul_schedctl_called = &udp->uberflags; 889 lmutex_unlock(&udp->link_lock); 890 891 ASSERT(self->ul_critical == 1); 892 ASSERT(self->ul_preempt == 0); 893 _lwp_terminate(); /* never returns */ 894 thr_panic("_thrp_exit(): _lwp_terminate() returned"); 895 } 896 897 void 898 collect_queue_statistics() 899 { 900 uberdata_t *udp = curthread->ul_uberdata; 901 ulwp_t *ulwp; 902 903 if (thread_queue_dump) { 904 lmutex_lock(&udp->link_lock); 905 if ((ulwp = udp->all_lwps) != NULL) { 906 do { 907 record_spin_locks(ulwp); 908 } while ((ulwp = ulwp->ul_forw) != udp->all_lwps); 909 } 910 lmutex_unlock(&udp->link_lock); 911 } 912 } 913 914 void 915 _thr_exit_common(void *status, int unwind) 916 { 917 ulwp_t *self = curthread; 918 int cancelled = (self->ul_cancel_pending && status == PTHREAD_CANCELED); 919 920 ASSERT(self->ul_critical == 0 && self->ul_preempt == 0); 921 922 /* 923 * Disable cancellation and call the special DCE cancellation 924 * cleanup hook if it is enabled. Do nothing else before calling 925 * the DCE cancellation cleanup hook; it may call longjmp() and 926 * never return here. 927 */ 928 self->ul_cancel_disabled = 1; 929 self->ul_cancel_async = 0; 930 self->ul_save_async = 0; 931 self->ul_cancelable = 0; 932 self->ul_cancel_pending = 0; 933 if (cancelled && cleanuphndlr != NULL) 934 (*cleanuphndlr)(); 935 936 /* 937 * Block application signals while we are exiting. 938 * We call out to C++, TSD, and TLS destructors while exiting 939 * and these are application-defined, so we cannot be assured 940 * that they won't reset the signal mask. We use sigoff() to 941 * defer any signals that may be received as a result of this 942 * bad behavior. Such signals will be lost to the process 943 * when the thread finishes exiting. 944 */ 945 (void) _thr_sigsetmask(SIG_SETMASK, &maskset, NULL); 946 sigoff(self); 947 948 self->ul_rval = status; 949 950 /* 951 * If thr_exit is being called from the places where 952 * C++ destructors are to be called such as cancellation 953 * points, then set this flag. It is checked in _t_cancel() 954 * to decide whether _ex_unwind() is to be called or not. 955 */ 956 if (unwind) 957 self->ul_unwind = 1; 958 959 /* 960 * _thrp_unwind() will eventually call _thrp_exit(). 961 * It never returns. 962 */ 963 _thrp_unwind(NULL); 964 thr_panic("_thr_exit_common(): _thrp_unwind() returned"); 965 } 966 967 /* 968 * Called when a thread returns from its start function. 969 * We are at the top of the stack; no unwinding is necessary. 970 */ 971 void 972 _thr_terminate(void *status) 973 { 974 _thr_exit_common(status, 0); 975 } 976 977 #pragma weak thr_exit = _thr_exit 978 #pragma weak pthread_exit = _thr_exit 979 #pragma weak _pthread_exit = _thr_exit 980 void 981 _thr_exit(void *status) 982 { 983 _thr_exit_common(status, 1); 984 } 985 986 int 987 _thrp_join(thread_t tid, thread_t *departed, void **status, int do_cancel) 988 { 989 uberdata_t *udp = curthread->ul_uberdata; 990 mutex_t *mp; 991 void *rval; 992 thread_t found; 993 ulwp_t *ulwp; 994 ulwp_t **ulwpp; 995 int replace; 996 int error; 997 998 if (do_cancel) 999 error = lwp_wait(tid, &found); 1000 else { 1001 while ((error = __lwp_wait(tid, &found)) == EINTR) 1002 ; 1003 } 1004 if (error) 1005 return (error); 1006 1007 /* 1008 * We must hold link_lock to avoid a race condition with find_stack(). 1009 */ 1010 lmutex_lock(&udp->link_lock); 1011 if ((ulwpp = find_lwpp(found)) == NULL) { 1012 /* 1013 * lwp_wait() found an lwp that the library doesn't know 1014 * about. It must have been created with _lwp_create(). 1015 * Just return its lwpid; we can't know its status. 1016 */ 1017 lmutex_unlock(&udp->link_lock); 1018 rval = NULL; 1019 } else { 1020 /* 1021 * Remove ulwp from the hash table. 1022 */ 1023 ulwp = *ulwpp; 1024 *ulwpp = ulwp->ul_hash; 1025 ulwp->ul_hash = NULL; 1026 /* 1027 * Remove ulwp from all_zombies list. 1028 */ 1029 ASSERT(udp->nzombies >= 1); 1030 if (udp->all_zombies == ulwp) 1031 udp->all_zombies = ulwp->ul_forw; 1032 if (udp->all_zombies == ulwp) 1033 udp->all_zombies = NULL; 1034 else { 1035 ulwp->ul_forw->ul_back = ulwp->ul_back; 1036 ulwp->ul_back->ul_forw = ulwp->ul_forw; 1037 } 1038 ulwp->ul_forw = ulwp->ul_back = NULL; 1039 udp->nzombies--; 1040 ASSERT(ulwp->ul_dead && !ulwp->ul_detached && 1041 !(ulwp->ul_usropts & (THR_DETACHED|THR_DAEMON))); 1042 /* 1043 * We can't call ulwp_unlock(ulwp) after we set 1044 * ulwp->ul_ix = -1 so we have to get a pointer to the 1045 * ulwp's hash table mutex now in order to unlock it below. 1046 */ 1047 mp = ulwp_mutex(ulwp, udp); 1048 ulwp->ul_lwpid = (lwpid_t)(-1); 1049 ulwp->ul_ix = -1; 1050 rval = ulwp->ul_rval; 1051 replace = ulwp->ul_replace; 1052 lmutex_unlock(mp); 1053 if (replace) { 1054 ulwp->ul_next = NULL; 1055 if (udp->ulwp_replace_free == NULL) 1056 udp->ulwp_replace_free = 1057 udp->ulwp_replace_last = ulwp; 1058 else { 1059 udp->ulwp_replace_last->ul_next = ulwp; 1060 udp->ulwp_replace_last = ulwp; 1061 } 1062 } 1063 lmutex_unlock(&udp->link_lock); 1064 } 1065 1066 if (departed != NULL) 1067 *departed = found; 1068 if (status != NULL) 1069 *status = rval; 1070 return (0); 1071 } 1072 1073 #pragma weak thr_join = _thr_join 1074 int 1075 _thr_join(thread_t tid, thread_t *departed, void **status) 1076 { 1077 int error = _thrp_join(tid, departed, status, 1); 1078 return ((error == EINVAL)? ESRCH : error); 1079 } 1080 1081 /* 1082 * pthread_join() differs from Solaris thr_join(): 1083 * It does not return the departed thread's id 1084 * and hence does not have a "departed" argument. 1085 * It returns EINVAL if tid refers to a detached thread. 1086 */ 1087 #pragma weak pthread_join = _pthread_join 1088 int 1089 _pthread_join(pthread_t tid, void **status) 1090 { 1091 return ((tid == 0)? ESRCH : _thrp_join(tid, NULL, status, 1)); 1092 } 1093 1094 #pragma weak pthread_detach = _thr_detach 1095 #pragma weak _pthread_detach = _thr_detach 1096 int 1097 _thr_detach(thread_t tid) 1098 { 1099 uberdata_t *udp = curthread->ul_uberdata; 1100 ulwp_t *ulwp; 1101 ulwp_t **ulwpp; 1102 int error = 0; 1103 1104 if ((ulwpp = find_lwpp(tid)) == NULL) 1105 return (ESRCH); 1106 ulwp = *ulwpp; 1107 1108 if (ulwp->ul_dead) { 1109 ulwp_unlock(ulwp, udp); 1110 error = _thrp_join(tid, NULL, NULL, 0); 1111 } else { 1112 error = __lwp_detach(tid); 1113 ulwp->ul_detached = 1; 1114 ulwp->ul_usropts |= THR_DETACHED; 1115 ulwp_unlock(ulwp, udp); 1116 } 1117 return (error); 1118 } 1119 1120 /* 1121 * Static local string compare function to avoid calling strncmp() 1122 * (and hence the dynamic linker) during library initialization. 1123 */ 1124 static int 1125 sncmp(const char *s1, const char *s2, size_t n) 1126 { 1127 n++; 1128 while (--n != 0 && *s1 == *s2++) 1129 if (*s1++ == '\0') 1130 return (0); 1131 return (n == 0 ? 0 : *(uchar_t *)s1 - *(uchar_t *)--s2); 1132 } 1133 1134 static const char * 1135 ematch(const char *ev, const char *match) 1136 { 1137 int c; 1138 1139 while ((c = *match++) != '\0') { 1140 if (*ev++ != c) 1141 return (NULL); 1142 } 1143 if (*ev++ != '=') 1144 return (NULL); 1145 return (ev); 1146 } 1147 1148 static int 1149 envvar(const char *ev, const char *match, int limit) 1150 { 1151 int val = -1; 1152 const char *ename; 1153 1154 if ((ename = ematch(ev, match)) != NULL) { 1155 int c; 1156 for (val = 0; (c = *ename) != '\0'; ename++) { 1157 if (!isdigit(c)) { 1158 val = -1; 1159 break; 1160 } 1161 val = val * 10 + (c - '0'); 1162 if (val > limit) { 1163 val = limit; 1164 break; 1165 } 1166 } 1167 } 1168 return (val); 1169 } 1170 1171 static void 1172 etest(const char *ev) 1173 { 1174 int value; 1175 1176 if ((value = envvar(ev, "QUEUE_SPIN", 1000000)) >= 0) 1177 thread_queue_spin = value; 1178 if ((value = envvar(ev, "ADAPTIVE_SPIN", 1000000)) >= 0) { 1179 thread_adaptive_spin = value; 1180 thread_release_spin = (value + 1) / 2; 1181 } 1182 if ((value = envvar(ev, "RELEASE_SPIN", 1000000)) >= 0) 1183 thread_release_spin = value; 1184 if ((value = envvar(ev, "MAX_SPINNERS", 100)) >= 0) 1185 thread_max_spinners = value; 1186 if ((value = envvar(ev, "QUEUE_FIFO", 8)) >= 0) 1187 thread_queue_fifo = value; 1188 #if defined(THREAD_DEBUG) 1189 if ((value = envvar(ev, "QUEUE_VERIFY", 1)) >= 0) 1190 thread_queue_verify = value; 1191 #endif 1192 if ((value = envvar(ev, "QUEUE_DUMP", 1)) >= 0) 1193 thread_queue_dump = value; 1194 if ((value = envvar(ev, "STACK_CACHE", 10000)) >= 0) 1195 thread_stack_cache = value; 1196 if ((value = envvar(ev, "COND_WAIT_DEFER", 1)) >= 0) 1197 thread_cond_wait_defer = value; 1198 if ((value = envvar(ev, "ERROR_DETECTION", 2)) >= 0) 1199 thread_error_detection = value; 1200 if ((value = envvar(ev, "ASYNC_SAFE", 1)) >= 0) 1201 thread_async_safe = value; 1202 if ((value = envvar(ev, "DOOR_NORESERVE", 1)) >= 0) 1203 thread_door_noreserve = value; 1204 } 1205 1206 /* 1207 * Look for and evaluate environment variables of the form "_THREAD_*". 1208 * For compatibility with the past, we also look for environment 1209 * names of the form "LIBTHREAD_*". 1210 */ 1211 static void 1212 set_thread_vars() 1213 { 1214 extern const char **_environ; 1215 const char **pev; 1216 const char *ev; 1217 char c; 1218 1219 if ((pev = _environ) == NULL) 1220 return; 1221 while ((ev = *pev++) != NULL) { 1222 c = *ev; 1223 if (c == '_' && sncmp(ev, "_THREAD_", 8) == 0) 1224 etest(ev + 8); 1225 if (c == 'L' && sncmp(ev, "LIBTHREAD_", 10) == 0) 1226 etest(ev + 10); 1227 } 1228 } 1229 1230 /* PROBE_SUPPORT begin */ 1231 #pragma weak __tnf_probe_notify 1232 extern void __tnf_probe_notify(void); 1233 /* PROBE_SUPPORT end */ 1234 1235 /* same as atexit() but private to the library */ 1236 extern int _atexit(void (*)(void)); 1237 1238 /* same as _cleanup() but private to the library */ 1239 extern void __cleanup(void); 1240 1241 extern void atfork_init(void); 1242 1243 #ifdef __amd64 1244 extern void __amd64id(void); 1245 #endif 1246 1247 /* 1248 * libc_init() is called by ld.so.1 for library initialization. 1249 * We perform minimal initialization; enough to work with the main thread. 1250 */ 1251 void 1252 libc_init(void) 1253 { 1254 uberdata_t *udp = &__uberdata; 1255 ulwp_t *oldself = __curthread(); 1256 ucontext_t uc; 1257 ulwp_t *self; 1258 struct rlimit rl; 1259 caddr_t data; 1260 size_t tls_size; 1261 int setmask; 1262 1263 /* 1264 * For the initial stage of initialization, we must be careful 1265 * not to call any function that could possibly call _cerror(). 1266 * For this purpose, we call only the raw system call wrappers. 1267 */ 1268 1269 #ifdef __amd64 1270 /* 1271 * Gather information about cache layouts for optimized 1272 * AMD assembler strfoo() and memfoo() functions. 1273 */ 1274 __amd64id(); 1275 #endif 1276 1277 /* 1278 * Every libc, regardless of which link map, must register __cleanup(). 1279 */ 1280 (void) _atexit(__cleanup); 1281 1282 /* 1283 * We keep our uberdata on one of (a) the first alternate link map 1284 * or (b) the primary link map. We switch to the primary link map 1285 * and stay there once we see it. All intermediate link maps are 1286 * subject to being unloaded at any time. 1287 */ 1288 if (oldself != NULL && (oldself->ul_primarymap || !primary_link_map)) { 1289 __tdb_bootstrap = oldself->ul_uberdata->tdb_bootstrap; 1290 mutex_setup(); 1291 atfork_init(); /* every link map needs atfork() processing */ 1292 return; 1293 } 1294 1295 /* 1296 * To establish the main stack information, we have to get our context. 1297 * This is also convenient to use for getting our signal mask. 1298 */ 1299 uc.uc_flags = UC_ALL; 1300 (void) __getcontext_syscall(&uc); 1301 ASSERT(uc.uc_link == NULL); 1302 1303 tls_size = roundup64(udp->tls_metadata.static_tls.tls_size); 1304 ASSERT(primary_link_map || tls_size == 0); 1305 data = lmalloc(sizeof (ulwp_t) + tls_size); 1306 if (data == NULL) 1307 thr_panic("cannot allocate thread structure for main thread"); 1308 /* LINTED pointer cast may result in improper alignment */ 1309 self = (ulwp_t *)(data + tls_size); 1310 init_hash_table[0].hash_bucket = self; 1311 1312 self->ul_sigmask = uc.uc_sigmask; 1313 delete_reserved_signals(&self->ul_sigmask); 1314 /* 1315 * Are the old and new sets different? 1316 * (This can happen if we are currently blocking SIGCANCEL.) 1317 * If so, we must explicitly set our signal mask, below. 1318 */ 1319 setmask = 1320 ((self->ul_sigmask.__sigbits[0] ^ uc.uc_sigmask.__sigbits[0]) | 1321 (self->ul_sigmask.__sigbits[1] ^ uc.uc_sigmask.__sigbits[1])); 1322 1323 #ifdef __sparc 1324 /* 1325 * We cache several instructions in the thread structure for use 1326 * by the fasttrap DTrace provider. When changing this, read the 1327 * comment in fasttrap.h for the all the other places that must 1328 * be changed. 1329 */ 1330 self->ul_dsave = 0x9de04000; /* save %g1, %g0, %sp */ 1331 self->ul_drestore = 0x81e80000; /* restore %g0, %g0, %g0 */ 1332 self->ul_dftret = 0x91d0203a; /* ta 0x3a */ 1333 self->ul_dreturn = 0x81ca0000; /* return %o0 */ 1334 #endif 1335 1336 self->ul_stktop = 1337 (uintptr_t)uc.uc_stack.ss_sp + uc.uc_stack.ss_size; 1338 (void) _private_getrlimit(RLIMIT_STACK, &rl); 1339 self->ul_stksiz = rl.rlim_cur; 1340 self->ul_stk = (caddr_t)(self->ul_stktop - self->ul_stksiz); 1341 1342 self->ul_forw = self->ul_back = self; 1343 self->ul_hash = NULL; 1344 self->ul_ix = 0; 1345 self->ul_lwpid = 1; /* __lwp_self() */ 1346 self->ul_main = 1; 1347 self->ul_self = self; 1348 self->ul_uberdata = udp; 1349 if (oldself != NULL) { 1350 int i; 1351 1352 ASSERT(primary_link_map); 1353 ASSERT(oldself->ul_main == 1); 1354 self->ul_stsd = oldself->ul_stsd; 1355 for (i = 0; i < TSD_NFAST; i++) 1356 self->ul_ftsd[i] = oldself->ul_ftsd[i]; 1357 self->ul_tls = oldself->ul_tls; 1358 /* 1359 * Retrieve all pointers to uberdata allocated 1360 * while running on previous link maps. 1361 * We would like to do a structure assignment here, but 1362 * gcc turns structure assignments into calls to memcpy(), 1363 * a function exported from libc. We can't call any such 1364 * external functions until we establish curthread, below, 1365 * so we just call our private version of memcpy(). 1366 */ 1367 (void) _private_memcpy(udp, 1368 oldself->ul_uberdata, sizeof (*udp)); 1369 /* 1370 * These items point to global data on the primary link map. 1371 */ 1372 udp->thr_hash_table = init_hash_table; 1373 udp->sigacthandler = sigacthandler; 1374 udp->tdb.tdb_events = tdb_events; 1375 ASSERT(udp->nthreads == 1 && !udp->uberflags.uf_mt); 1376 ASSERT(udp->lwp_stacks == NULL); 1377 ASSERT(udp->ulwp_freelist == NULL); 1378 ASSERT(udp->ulwp_replace_free == NULL); 1379 ASSERT(udp->hash_size == 1); 1380 } 1381 udp->all_lwps = self; 1382 udp->ulwp_one = self; 1383 udp->pid = _private_getpid(); 1384 udp->nthreads = 1; 1385 /* 1386 * In every link map, tdb_bootstrap points to the same piece of 1387 * allocated memory. When the primary link map is initialized, 1388 * the allocated memory is assigned a pointer to the one true 1389 * uberdata. This allows libc_db to initialize itself regardless 1390 * of which instance of libc it finds in the address space. 1391 */ 1392 if (udp->tdb_bootstrap == NULL) 1393 udp->tdb_bootstrap = lmalloc(sizeof (uberdata_t *)); 1394 __tdb_bootstrap = udp->tdb_bootstrap; 1395 if (primary_link_map) { 1396 self->ul_primarymap = 1; 1397 udp->primary_map = 1; 1398 *udp->tdb_bootstrap = udp; 1399 } 1400 /* 1401 * Cancellation can't happen until: 1402 * pthread_cancel() is called 1403 * or: 1404 * another thread is created 1405 * For now, as a single-threaded process, set the flag that tells 1406 * PROLOGUE/EPILOGUE (in scalls.c) that cancellation can't happen. 1407 */ 1408 self->ul_nocancel = 1; 1409 1410 #if defined(__amd64) 1411 self->ul_gs = ___lwp_private(_LWP_SETPRIVATE, _LWP_FSBASE, self); 1412 #elif defined(__i386) 1413 self->ul_gs = ___lwp_private(_LWP_SETPRIVATE, _LWP_GSBASE, self); 1414 #endif /* __i386 || __amd64 */ 1415 set_curthread(self); /* redundant on i386 */ 1416 /* 1417 * Now curthread is established and it is safe to call any 1418 * function in libc except one that uses thread-local storage. 1419 */ 1420 self->ul_errnop = &errno; 1421 if (oldself != NULL) { 1422 /* tls_size was zero when oldself was allocated */ 1423 lfree(oldself, sizeof (ulwp_t)); 1424 } 1425 mutex_setup(); 1426 atfork_init(); 1427 signal_init(); 1428 1429 /* 1430 * If the stack is unlimited, we set the size to zero to disable 1431 * stack checking. 1432 * XXX: Work harder here. Get the stack size from /proc/self/rmap 1433 */ 1434 if (self->ul_stksiz == RLIM_INFINITY) { 1435 self->ul_ustack.ss_sp = (void *)self->ul_stktop; 1436 self->ul_ustack.ss_size = 0; 1437 } else { 1438 self->ul_ustack.ss_sp = self->ul_stk; 1439 self->ul_ustack.ss_size = self->ul_stksiz; 1440 } 1441 self->ul_ustack.ss_flags = 0; 1442 (void) _private_setustack(&self->ul_ustack); 1443 1444 /* 1445 * Get the variables that affect thread behavior from the environment. 1446 */ 1447 set_thread_vars(); 1448 udp->uberflags.uf_thread_error_detection = (char)thread_error_detection; 1449 udp->thread_stack_cache = thread_stack_cache; 1450 1451 /* 1452 * Make per-thread copies of global variables, for speed. 1453 */ 1454 self->ul_queue_fifo = (char)thread_queue_fifo; 1455 self->ul_cond_wait_defer = (char)thread_cond_wait_defer; 1456 self->ul_error_detection = (char)thread_error_detection; 1457 self->ul_async_safe = (char)thread_async_safe; 1458 self->ul_door_noreserve = (char)thread_door_noreserve; 1459 self->ul_max_spinners = (uchar_t)thread_max_spinners; 1460 self->ul_adaptive_spin = thread_adaptive_spin; 1461 self->ul_release_spin = thread_release_spin; 1462 self->ul_queue_spin = thread_queue_spin; 1463 1464 /* 1465 * When we have initialized the primary link map, inform 1466 * the dynamic linker about our interface functions. 1467 */ 1468 if (self->ul_primarymap) 1469 _ld_libc((void *)rtld_funcs); 1470 1471 /* 1472 * Defer signals until TLS constructors have been called. 1473 */ 1474 sigoff(self); 1475 tls_setup(); 1476 sigon(self); 1477 if (setmask) 1478 (void) restore_signals(self); 1479 1480 /* PROBE_SUPPORT begin */ 1481 if (self->ul_primarymap && __tnf_probe_notify != NULL) 1482 __tnf_probe_notify(); 1483 /* PROBE_SUPPORT end */ 1484 } 1485 1486 #pragma fini(libc_fini) 1487 void 1488 libc_fini() 1489 { 1490 /* 1491 * If we are doing fini processing for the instance of libc 1492 * on the first alternate link map (this happens only when 1493 * the dynamic linker rejects a bad audit library), then clear 1494 * __curthread(). We abandon whatever memory was allocated by 1495 * lmalloc() while running on this alternate link-map but we 1496 * don't care (and can't find the memory in any case); we just 1497 * want to protect the application from this bad audit library. 1498 * No fini processing is done by libc in the normal case. 1499 */ 1500 1501 uberdata_t *udp = curthread->ul_uberdata; 1502 1503 if (udp->primary_map == 0 && udp == &__uberdata) 1504 set_curthread(NULL); 1505 } 1506 1507 /* 1508 * finish_init is called when we are about to become multi-threaded, 1509 * that is, on the first call to thr_create(). 1510 */ 1511 void 1512 finish_init() 1513 { 1514 ulwp_t *self = curthread; 1515 uberdata_t *udp = self->ul_uberdata; 1516 thr_hash_table_t *htp; 1517 void *data; 1518 int i; 1519 1520 /* 1521 * No locks needed here; we are single-threaded on the first call. 1522 * We can be called only after the primary link map has been set up. 1523 */ 1524 ASSERT(self->ul_primarymap); 1525 ASSERT(self == udp->ulwp_one); 1526 ASSERT(!udp->uberflags.uf_mt); 1527 ASSERT(udp->hash_size == 1); 1528 1529 /* 1530 * First allocate the queue_head array if not already allocated. 1531 */ 1532 if (udp->queue_head == NULL) 1533 queue_alloc(); 1534 1535 /* 1536 * Now allocate the thread hash table. 1537 */ 1538 if ((data = _private_mmap(NULL, HASHTBLSZ * sizeof (thr_hash_table_t), 1539 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, (off_t)0)) 1540 == MAP_FAILED) 1541 thr_panic("cannot allocate thread hash table"); 1542 1543 udp->thr_hash_table = htp = (thr_hash_table_t *)data; 1544 udp->hash_size = HASHTBLSZ; 1545 udp->hash_mask = HASHTBLSZ - 1; 1546 1547 for (i = 0; i < HASHTBLSZ; i++, htp++) { 1548 htp->hash_lock.mutex_magic = MUTEX_MAGIC; 1549 htp->hash_cond.cond_magic = COND_MAGIC; 1550 } 1551 hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp); 1552 1553 /* 1554 * Set up the SIGCANCEL handler for threads cancellation. 1555 */ 1556 init_sigcancel(); 1557 1558 /* 1559 * Arrange to do special things on exit -- 1560 * - collect queue statistics from all remaining active threads. 1561 * - grab assert_lock to ensure that assertion failures 1562 * and a core dump take precedence over _exit(). 1563 * - dump queue statistics to stderr if _THREAD_QUEUE_DUMP is set. 1564 * (Functions are called in the reverse order of their registration.) 1565 */ 1566 (void) _atexit(dump_queue_statistics); 1567 (void) _atexit(grab_assert_lock); 1568 (void) _atexit(collect_queue_statistics); 1569 } 1570 1571 /* 1572 * Used only by _postfork1_child(), below. 1573 */ 1574 static void 1575 mark_dead_and_buried(ulwp_t *ulwp) 1576 { 1577 ulwp->ul_dead = 1; 1578 ulwp->ul_lwpid = (lwpid_t)(-1); 1579 ulwp->ul_hash = NULL; 1580 ulwp->ul_ix = -1; 1581 ulwp->ul_schedctl = NULL; 1582 ulwp->ul_schedctl_called = NULL; 1583 } 1584 1585 /* 1586 * This is called from fork1() in the child. 1587 * Reset our data structures to reflect one lwp. 1588 */ 1589 void 1590 _postfork1_child() 1591 { 1592 ulwp_t *self = curthread; 1593 uberdata_t *udp = self->ul_uberdata; 1594 ulwp_t *next; 1595 ulwp_t *ulwp; 1596 int i; 1597 1598 /* daemon threads shouldn't call fork1(), but oh well... */ 1599 self->ul_usropts &= ~THR_DAEMON; 1600 udp->nthreads = 1; 1601 udp->ndaemons = 0; 1602 udp->uberflags.uf_mt = 0; 1603 __threaded = 0; 1604 for (i = 0; i < udp->hash_size; i++) 1605 udp->thr_hash_table[i].hash_bucket = NULL; 1606 self->ul_lwpid = __lwp_self(); 1607 hash_in_unlocked(self, TIDHASH(self->ul_lwpid, udp), udp); 1608 1609 /* no one in the child is on a sleep queue; reinitialize */ 1610 if (udp->queue_head) { 1611 (void) _private_memset(udp->queue_head, 0, 1612 2 * QHASHSIZE * sizeof (queue_head_t)); 1613 for (i = 0; i < 2 * QHASHSIZE; i++) 1614 udp->queue_head[i].qh_lock.mutex_magic = MUTEX_MAGIC; 1615 } 1616 1617 /* 1618 * All lwps except ourself are gone. Mark them so. 1619 * First mark all of the lwps that have already been freed. 1620 * Then mark and free all of the active lwps except ourself. 1621 * Since we are single-threaded, no locks are required here. 1622 */ 1623 for (ulwp = udp->lwp_stacks; ulwp != NULL; ulwp = ulwp->ul_next) 1624 mark_dead_and_buried(ulwp); 1625 for (ulwp = udp->ulwp_freelist; ulwp != NULL; ulwp = ulwp->ul_next) 1626 mark_dead_and_buried(ulwp); 1627 for (ulwp = self->ul_forw; ulwp != self; ulwp = next) { 1628 next = ulwp->ul_forw; 1629 ulwp->ul_forw = ulwp->ul_back = NULL; 1630 mark_dead_and_buried(ulwp); 1631 tsd_free(ulwp); 1632 tls_free(ulwp); 1633 rwl_free(ulwp); 1634 ulwp_free(ulwp); 1635 } 1636 self->ul_forw = self->ul_back = udp->all_lwps = self; 1637 if (self != udp->ulwp_one) 1638 mark_dead_and_buried(udp->ulwp_one); 1639 if ((ulwp = udp->all_zombies) != NULL) { 1640 ASSERT(udp->nzombies != 0); 1641 do { 1642 next = ulwp->ul_forw; 1643 ulwp->ul_forw = ulwp->ul_back = NULL; 1644 mark_dead_and_buried(ulwp); 1645 udp->nzombies--; 1646 if (ulwp->ul_replace) { 1647 ulwp->ul_next = NULL; 1648 if (udp->ulwp_replace_free == NULL) { 1649 udp->ulwp_replace_free = 1650 udp->ulwp_replace_last = ulwp; 1651 } else { 1652 udp->ulwp_replace_last->ul_next = ulwp; 1653 udp->ulwp_replace_last = ulwp; 1654 } 1655 } 1656 } while ((ulwp = next) != udp->all_zombies); 1657 ASSERT(udp->nzombies == 0); 1658 udp->all_zombies = NULL; 1659 udp->nzombies = 0; 1660 } 1661 trim_stack_cache(0); 1662 } 1663 1664 #pragma weak thr_setprio = _thr_setprio 1665 #pragma weak pthread_setschedprio = _thr_setprio 1666 #pragma weak _pthread_setschedprio = _thr_setprio 1667 int 1668 _thr_setprio(thread_t tid, int priority) 1669 { 1670 struct sched_param param; 1671 1672 (void) _memset(¶m, 0, sizeof (param)); 1673 param.sched_priority = priority; 1674 return (_thread_setschedparam_main(tid, 0, ¶m, PRIO_SET_PRIO)); 1675 } 1676 1677 #pragma weak thr_getprio = _thr_getprio 1678 int 1679 _thr_getprio(thread_t tid, int *priority) 1680 { 1681 uberdata_t *udp = curthread->ul_uberdata; 1682 ulwp_t *ulwp; 1683 int error = 0; 1684 1685 if ((ulwp = find_lwp(tid)) == NULL) 1686 error = ESRCH; 1687 else { 1688 *priority = ulwp->ul_pri; 1689 ulwp_unlock(ulwp, udp); 1690 } 1691 return (error); 1692 } 1693 1694 lwpid_t 1695 lwp_self(void) 1696 { 1697 return (curthread->ul_lwpid); 1698 } 1699 1700 #pragma weak _ti_thr_self = _thr_self 1701 #pragma weak thr_self = _thr_self 1702 #pragma weak pthread_self = _thr_self 1703 #pragma weak _pthread_self = _thr_self 1704 thread_t 1705 _thr_self() 1706 { 1707 return (curthread->ul_lwpid); 1708 } 1709 1710 #pragma weak thr_main = _thr_main 1711 int 1712 _thr_main() 1713 { 1714 ulwp_t *self = __curthread(); 1715 1716 return ((self == NULL)? -1 : self->ul_main); 1717 } 1718 1719 int 1720 _thrp_stksegment(ulwp_t *ulwp, stack_t *stk) 1721 { 1722 stk->ss_sp = (void *)ulwp->ul_stktop; 1723 stk->ss_size = ulwp->ul_stksiz; 1724 stk->ss_flags = 0; 1725 return (0); 1726 } 1727 1728 #pragma weak thr_stksegment = _thr_stksegment 1729 int 1730 _thr_stksegment(stack_t *stk) 1731 { 1732 return (_thrp_stksegment(curthread, stk)); 1733 } 1734 1735 void 1736 force_continue(ulwp_t *ulwp) 1737 { 1738 #if defined(THREAD_DEBUG) 1739 ulwp_t *self = curthread; 1740 uberdata_t *udp = self->ul_uberdata; 1741 #endif 1742 int error; 1743 timespec_t ts; 1744 1745 ASSERT(MUTEX_OWNED(ulwp_mutex(ulwp, udp), self)); 1746 1747 for (;;) { 1748 error = __lwp_continue(ulwp->ul_lwpid); 1749 if (error != 0 && error != EINTR) 1750 break; 1751 error = 0; 1752 if (ulwp->ul_stopping) { /* he is stopping himself */ 1753 ts.tv_sec = 0; /* give him a chance to run */ 1754 ts.tv_nsec = 100000; /* 100 usecs or clock tick */ 1755 (void) ___nanosleep(&ts, NULL); 1756 } 1757 if (!ulwp->ul_stopping) /* he is running now */ 1758 break; /* so we are done */ 1759 /* 1760 * He is marked as being in the process of stopping 1761 * himself. Loop around and continue him again. 1762 * He may not have been stopped the first time. 1763 */ 1764 } 1765 } 1766 1767 /* 1768 * Suspend an lwp with lwp_suspend(), then move it to a safe 1769 * point, that is, to a point where ul_critical is zero. 1770 * On return, the ulwp_lock() is dropped as with ulwp_unlock(). 1771 * If 'link_dropped' is non-NULL, then 'link_lock' is held on entry. 1772 * If we have to drop link_lock, we store 1 through link_dropped. 1773 * If the lwp exits before it can be suspended, we return ESRCH. 1774 */ 1775 int 1776 safe_suspend(ulwp_t *ulwp, uchar_t whystopped, int *link_dropped) 1777 { 1778 ulwp_t *self = curthread; 1779 uberdata_t *udp = self->ul_uberdata; 1780 cond_t *cvp = ulwp_condvar(ulwp, udp); 1781 mutex_t *mp = ulwp_mutex(ulwp, udp); 1782 thread_t tid = ulwp->ul_lwpid; 1783 int ix = ulwp->ul_ix; 1784 int error = 0; 1785 1786 ASSERT(whystopped == TSTP_REGULAR || 1787 whystopped == TSTP_MUTATOR || 1788 whystopped == TSTP_FORK); 1789 ASSERT(ulwp != self); 1790 ASSERT(!ulwp->ul_stop); 1791 ASSERT(MUTEX_OWNED(mp, self)); 1792 1793 if (link_dropped != NULL) 1794 *link_dropped = 0; 1795 1796 /* 1797 * We must grab the target's spin lock before suspending it. 1798 * See the comments below and in _thrp_suspend() for why. 1799 */ 1800 spin_lock_set(&ulwp->ul_spinlock); 1801 (void) ___lwp_suspend(tid); 1802 spin_lock_clear(&ulwp->ul_spinlock); 1803 1804 top: 1805 if (ulwp->ul_critical == 0 || ulwp->ul_stopping) { 1806 /* thread is already safe */ 1807 ulwp->ul_stop |= whystopped; 1808 } else { 1809 /* 1810 * Setting ul_pleasestop causes the target thread to stop 1811 * itself in _thrp_suspend(), below, after we drop its lock. 1812 * We must continue the critical thread before dropping 1813 * link_lock because the critical thread may be holding 1814 * the queue lock for link_lock. This is delicate. 1815 */ 1816 ulwp->ul_pleasestop |= whystopped; 1817 force_continue(ulwp); 1818 if (link_dropped != NULL) { 1819 *link_dropped = 1; 1820 lmutex_unlock(&udp->link_lock); 1821 /* be sure to drop link_lock only once */ 1822 link_dropped = NULL; 1823 } 1824 1825 /* 1826 * The thread may disappear by calling thr_exit() so we 1827 * cannot rely on the ulwp pointer after dropping the lock. 1828 * Instead, we search the hash table to find it again. 1829 * When we return, we may find that the thread has been 1830 * continued by some other thread. The suspend/continue 1831 * interfaces are prone to such race conditions by design. 1832 */ 1833 while (ulwp && !ulwp->ul_dead && !ulwp->ul_stop && 1834 (ulwp->ul_pleasestop & whystopped)) { 1835 (void) _cond_wait(cvp, mp); 1836 for (ulwp = udp->thr_hash_table[ix].hash_bucket; 1837 ulwp != NULL; ulwp = ulwp->ul_hash) { 1838 if (ulwp->ul_lwpid == tid) 1839 break; 1840 } 1841 } 1842 1843 if (ulwp == NULL || ulwp->ul_dead) 1844 error = ESRCH; 1845 else { 1846 /* 1847 * Do another lwp_suspend() to make sure we don't 1848 * return until the target thread is fully stopped 1849 * in the kernel. Don't apply lwp_suspend() until 1850 * we know that the target is not holding any 1851 * queue locks, that is, that it has completed 1852 * ulwp_unlock(self) and has, or at least is 1853 * about to, call lwp_suspend() on itself. We do 1854 * this by grabbing the target's spin lock. 1855 */ 1856 ASSERT(ulwp->ul_lwpid == tid); 1857 spin_lock_set(&ulwp->ul_spinlock); 1858 (void) ___lwp_suspend(tid); 1859 spin_lock_clear(&ulwp->ul_spinlock); 1860 /* 1861 * If some other thread did a thr_continue() 1862 * on the target thread we have to start over. 1863 */ 1864 if (!ulwp->ul_stopping || !(ulwp->ul_stop & whystopped)) 1865 goto top; 1866 } 1867 } 1868 1869 (void) cond_broadcast_internal(cvp); 1870 lmutex_unlock(mp); 1871 return (error); 1872 } 1873 1874 int 1875 _thrp_suspend(thread_t tid, uchar_t whystopped) 1876 { 1877 ulwp_t *self = curthread; 1878 uberdata_t *udp = self->ul_uberdata; 1879 ulwp_t *ulwp; 1880 int error = 0; 1881 1882 ASSERT((whystopped & (TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) != 0); 1883 ASSERT((whystopped & ~(TSTP_REGULAR|TSTP_MUTATOR|TSTP_FORK)) == 0); 1884 1885 /* 1886 * We can't suspend anyone except ourself while a fork is happening. 1887 * This also has the effect of allowing only one suspension at a time. 1888 */ 1889 if (tid != self->ul_lwpid) 1890 (void) fork_lock_enter(NULL); 1891 1892 if ((ulwp = find_lwp(tid)) == NULL) 1893 error = ESRCH; 1894 else if (whystopped == TSTP_MUTATOR && !ulwp->ul_mutator) { 1895 ulwp_unlock(ulwp, udp); 1896 error = EINVAL; 1897 } else if (ulwp->ul_stop) { /* already stopped */ 1898 ulwp->ul_stop |= whystopped; 1899 ulwp_broadcast(ulwp); 1900 ulwp_unlock(ulwp, udp); 1901 } else if (ulwp != self) { 1902 /* 1903 * After suspending the other thread, move it out of a 1904 * critical section and deal with the schedctl mappings. 1905 * safe_suspend() suspends the other thread, calls 1906 * ulwp_broadcast(ulwp) and drops the ulwp lock. 1907 */ 1908 error = safe_suspend(ulwp, whystopped, NULL); 1909 } else { 1910 int schedctl_after_fork = 0; 1911 1912 /* 1913 * We are suspending ourself. We must not take a signal 1914 * until we return from lwp_suspend() and clear ul_stopping. 1915 * This is to guard against siglongjmp(). 1916 */ 1917 enter_critical(self); 1918 self->ul_sp = stkptr(); 1919 _flush_windows(); /* sparc */ 1920 self->ul_pleasestop = 0; 1921 self->ul_stop |= whystopped; 1922 /* 1923 * Grab our spin lock before dropping ulwp_mutex(self). 1924 * This prevents the suspending thread from applying 1925 * lwp_suspend() to us before we emerge from 1926 * lmutex_unlock(mp) and have dropped mp's queue lock. 1927 */ 1928 spin_lock_set(&self->ul_spinlock); 1929 self->ul_stopping = 1; 1930 ulwp_broadcast(self); 1931 ulwp_unlock(self, udp); 1932 /* 1933 * From this point until we return from lwp_suspend(), 1934 * we must not call any function that might invoke the 1935 * dynamic linker, that is, we can only call functions 1936 * private to the library. 1937 * 1938 * Also, this is a nasty race condition for a process 1939 * that is undergoing a forkall() operation: 1940 * Once we clear our spinlock (below), we are vulnerable 1941 * to being suspended by the forkall() thread before 1942 * we manage to suspend ourself in ___lwp_suspend(). 1943 * See safe_suspend() and force_continue(). 1944 * 1945 * To avoid a SIGSEGV due to the disappearance 1946 * of the schedctl mappings in the child process, 1947 * which can happen in spin_lock_clear() if we 1948 * are suspended while we are in the middle of 1949 * its call to preempt(), we preemptively clear 1950 * our own schedctl pointer before dropping our 1951 * spinlock. We reinstate it, in both the parent 1952 * and (if this really is a forkall()) the child. 1953 */ 1954 if (whystopped & TSTP_FORK) { 1955 schedctl_after_fork = 1; 1956 self->ul_schedctl = NULL; 1957 self->ul_schedctl_called = &udp->uberflags; 1958 } 1959 spin_lock_clear(&self->ul_spinlock); 1960 (void) ___lwp_suspend(tid); 1961 /* 1962 * Somebody else continued us. 1963 * We can't grab ulwp_lock(self) 1964 * until after clearing ul_stopping. 1965 * force_continue() relies on this. 1966 */ 1967 self->ul_stopping = 0; 1968 self->ul_sp = 0; 1969 if (schedctl_after_fork) { 1970 self->ul_schedctl_called = NULL; 1971 self->ul_schedctl = NULL; 1972 (void) setup_schedctl(); 1973 } 1974 ulwp_lock(self, udp); 1975 ulwp_broadcast(self); 1976 ulwp_unlock(self, udp); 1977 exit_critical(self); 1978 } 1979 1980 if (tid != self->ul_lwpid) 1981 fork_lock_exit(); 1982 1983 return (error); 1984 } 1985 1986 /* 1987 * Suspend all lwps other than ourself in preparation for fork. 1988 */ 1989 void 1990 suspend_fork() 1991 { 1992 ulwp_t *self = curthread; 1993 uberdata_t *udp = self->ul_uberdata; 1994 ulwp_t *ulwp; 1995 int link_dropped; 1996 1997 top: 1998 lmutex_lock(&udp->link_lock); 1999 2000 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) { 2001 ulwp_lock(ulwp, udp); 2002 if (ulwp->ul_stop) { /* already stopped */ 2003 ulwp->ul_stop |= TSTP_FORK; 2004 ulwp_broadcast(ulwp); 2005 ulwp_unlock(ulwp, udp); 2006 } else { 2007 /* 2008 * Move the stopped lwp out of a critical section. 2009 */ 2010 if (safe_suspend(ulwp, TSTP_FORK, &link_dropped) || 2011 link_dropped) 2012 goto top; 2013 } 2014 } 2015 2016 lmutex_unlock(&udp->link_lock); 2017 } 2018 2019 void 2020 continue_fork(int child) 2021 { 2022 ulwp_t *self = curthread; 2023 uberdata_t *udp = self->ul_uberdata; 2024 ulwp_t *ulwp; 2025 2026 /* 2027 * Clear the schedctl pointers in the child of forkall(). 2028 */ 2029 if (child) { 2030 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) { 2031 ulwp->ul_schedctl_called = 2032 ulwp->ul_dead? &udp->uberflags : NULL; 2033 ulwp->ul_schedctl = NULL; 2034 } 2035 } 2036 2037 /* 2038 * Set all lwps that were stopped for fork() running again. 2039 */ 2040 lmutex_lock(&udp->link_lock); 2041 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) { 2042 mutex_t *mp = ulwp_mutex(ulwp, udp); 2043 lmutex_lock(mp); 2044 ASSERT(ulwp->ul_stop & TSTP_FORK); 2045 ulwp->ul_stop &= ~TSTP_FORK; 2046 ulwp_broadcast(ulwp); 2047 if (!ulwp->ul_stop) 2048 force_continue(ulwp); 2049 lmutex_unlock(mp); 2050 } 2051 lmutex_unlock(&udp->link_lock); 2052 } 2053 2054 int 2055 _thrp_continue(thread_t tid, uchar_t whystopped) 2056 { 2057 uberdata_t *udp = curthread->ul_uberdata; 2058 ulwp_t *ulwp; 2059 mutex_t *mp; 2060 int error = 0; 2061 2062 ASSERT(whystopped == TSTP_REGULAR || 2063 whystopped == TSTP_MUTATOR); 2064 2065 if ((ulwp = find_lwp(tid)) == NULL) 2066 return (ESRCH); 2067 2068 mp = ulwp_mutex(ulwp, udp); 2069 if ((whystopped == TSTP_MUTATOR && !ulwp->ul_mutator)) { 2070 error = EINVAL; 2071 } else if (ulwp->ul_stop & whystopped) { 2072 ulwp->ul_stop &= ~whystopped; 2073 ulwp_broadcast(ulwp); 2074 if (!ulwp->ul_stop) { 2075 if (whystopped == TSTP_REGULAR && ulwp->ul_created) { 2076 ulwp->ul_sp = 0; 2077 ulwp->ul_created = 0; 2078 } 2079 force_continue(ulwp); 2080 } 2081 } 2082 2083 lmutex_unlock(mp); 2084 return (error); 2085 } 2086 2087 #pragma weak thr_suspend = _thr_suspend 2088 int 2089 _thr_suspend(thread_t tid) 2090 { 2091 return (_thrp_suspend(tid, TSTP_REGULAR)); 2092 } 2093 2094 #pragma weak thr_continue = _thr_continue 2095 int 2096 _thr_continue(thread_t tid) 2097 { 2098 return (_thrp_continue(tid, TSTP_REGULAR)); 2099 } 2100 2101 #pragma weak thr_yield = _thr_yield 2102 void 2103 _thr_yield() 2104 { 2105 lwp_yield(); 2106 } 2107 2108 #pragma weak thr_kill = _thr_kill 2109 #pragma weak pthread_kill = _thr_kill 2110 #pragma weak _pthread_kill = _thr_kill 2111 int 2112 _thr_kill(thread_t tid, int sig) 2113 { 2114 if (sig == SIGCANCEL) 2115 return (EINVAL); 2116 return (__lwp_kill(tid, sig)); 2117 } 2118 2119 /* 2120 * Exit a critical section, take deferred actions if necessary. 2121 */ 2122 void 2123 do_exit_critical() 2124 { 2125 ulwp_t *self = curthread; 2126 int sig; 2127 2128 ASSERT(self->ul_critical == 0); 2129 if (self->ul_dead) 2130 return; 2131 2132 while (self->ul_pleasestop || 2133 (self->ul_cursig != 0 && self->ul_sigdefer == 0)) { 2134 /* 2135 * Avoid a recursive call to exit_critical() in _thrp_suspend() 2136 * by keeping self->ul_critical == 1 here. 2137 */ 2138 self->ul_critical++; 2139 while (self->ul_pleasestop) { 2140 /* 2141 * Guard against suspending ourself while on a sleep 2142 * queue. See the comments in call_user_handler(). 2143 */ 2144 unsleep_self(); 2145 set_parking_flag(self, 0); 2146 (void) _thrp_suspend(self->ul_lwpid, 2147 self->ul_pleasestop); 2148 } 2149 self->ul_critical--; 2150 2151 if ((sig = self->ul_cursig) != 0 && self->ul_sigdefer == 0) { 2152 /* 2153 * Clear ul_cursig before proceeding. 2154 * This protects us from the dynamic linker's 2155 * calls to bind_guard()/bind_clear() in the 2156 * event that it is invoked to resolve a symbol 2157 * like take_deferred_signal() below. 2158 */ 2159 self->ul_cursig = 0; 2160 take_deferred_signal(sig); 2161 ASSERT(self->ul_cursig == 0); 2162 } 2163 } 2164 ASSERT(self->ul_critical == 0); 2165 } 2166 2167 int 2168 _ti_bind_guard(int bindflag) 2169 { 2170 ulwp_t *self = curthread; 2171 2172 if ((self->ul_bindflags & bindflag) == bindflag) 2173 return (0); 2174 enter_critical(self); 2175 self->ul_bindflags |= bindflag; 2176 return (1); 2177 } 2178 2179 int 2180 _ti_bind_clear(int bindflag) 2181 { 2182 ulwp_t *self = curthread; 2183 2184 if ((self->ul_bindflags & bindflag) == 0) 2185 return (self->ul_bindflags); 2186 self->ul_bindflags &= ~bindflag; 2187 exit_critical(self); 2188 return (self->ul_bindflags); 2189 } 2190 2191 /* 2192 * sigoff() and sigon() enable cond_wait() to behave (optionally) like 2193 * it does in the old libthread (see the comments in cond_wait_queue()). 2194 * Also, signals are deferred at thread startup until TLS constructors 2195 * have all been called, at which time _thr_setup() calls sigon(). 2196 * Also, _sigoff() and _sigon() are called from dbx's run-time checking 2197 * (librtc.so) to defer signals during its critical sections (not to be 2198 * confused with libc critical sections [see exit_critical() above]). 2199 */ 2200 void 2201 _sigoff(void) 2202 { 2203 sigoff(curthread); 2204 } 2205 2206 void 2207 _sigon(void) 2208 { 2209 sigon(curthread); 2210 } 2211 2212 void 2213 sigon(ulwp_t *self) 2214 { 2215 int sig; 2216 2217 ASSERT(self->ul_sigdefer > 0); 2218 if (--self->ul_sigdefer == 0) { 2219 if ((sig = self->ul_cursig) != 0 && self->ul_critical == 0) { 2220 self->ul_cursig = 0; 2221 take_deferred_signal(sig); 2222 ASSERT(self->ul_cursig == 0); 2223 } 2224 } 2225 } 2226 2227 #pragma weak thr_getconcurrency = _thr_getconcurrency 2228 int 2229 _thr_getconcurrency() 2230 { 2231 return (thr_concurrency); 2232 } 2233 2234 #pragma weak pthread_getconcurrency = _pthread_getconcurrency 2235 int 2236 _pthread_getconcurrency() 2237 { 2238 return (pthread_concurrency); 2239 } 2240 2241 #pragma weak thr_setconcurrency = _thr_setconcurrency 2242 int 2243 _thr_setconcurrency(int new_level) 2244 { 2245 uberdata_t *udp = curthread->ul_uberdata; 2246 2247 if (new_level < 0) 2248 return (EINVAL); 2249 if (new_level > 65536) /* 65536 is totally arbitrary */ 2250 return (EAGAIN); 2251 lmutex_lock(&udp->link_lock); 2252 if (new_level > thr_concurrency) 2253 thr_concurrency = new_level; 2254 lmutex_unlock(&udp->link_lock); 2255 return (0); 2256 } 2257 2258 #pragma weak pthread_setconcurrency = _pthread_setconcurrency 2259 int 2260 _pthread_setconcurrency(int new_level) 2261 { 2262 if (new_level < 0) 2263 return (EINVAL); 2264 if (new_level > 65536) /* 65536 is totally arbitrary */ 2265 return (EAGAIN); 2266 pthread_concurrency = new_level; 2267 return (0); 2268 } 2269 2270 #pragma weak thr_min_stack = _thr_min_stack 2271 #pragma weak __pthread_min_stack = _thr_min_stack 2272 size_t 2273 _thr_min_stack(void) 2274 { 2275 return (MINSTACK); 2276 } 2277 2278 int 2279 __nthreads(void) 2280 { 2281 return (curthread->ul_uberdata->nthreads); 2282 } 2283 2284 /* 2285 * XXX 2286 * The remainder of this file implements the private interfaces to java for 2287 * garbage collection. It is no longer used, at least by java 1.2. 2288 * It can all go away once all old JVMs have disappeared. 2289 */ 2290 2291 int suspendingallmutators; /* when non-zero, suspending all mutators. */ 2292 int suspendedallmutators; /* when non-zero, all mutators suspended. */ 2293 int mutatorsbarrier; /* when non-zero, mutators barrier imposed. */ 2294 mutex_t mutatorslock = DEFAULTMUTEX; /* used to enforce mutators barrier. */ 2295 cond_t mutatorscv = DEFAULTCV; /* where non-mutators sleep. */ 2296 2297 /* 2298 * Get the available register state for the target thread. 2299 * Return non-volatile registers: TRS_NONVOLATILE 2300 */ 2301 #pragma weak thr_getstate = _thr_getstate 2302 int 2303 _thr_getstate(thread_t tid, int *flag, lwpid_t *lwp, stack_t *ss, gregset_t rs) 2304 { 2305 ulwp_t *self = curthread; 2306 uberdata_t *udp = self->ul_uberdata; 2307 ulwp_t **ulwpp; 2308 ulwp_t *ulwp; 2309 int error = 0; 2310 int trs_flag = TRS_LWPID; 2311 2312 if (tid == 0 || self->ul_lwpid == tid) { 2313 ulwp = self; 2314 ulwp_lock(ulwp, udp); 2315 } else if ((ulwpp = find_lwpp(tid)) != NULL) { 2316 ulwp = *ulwpp; 2317 } else { 2318 if (flag) 2319 *flag = TRS_INVALID; 2320 return (ESRCH); 2321 } 2322 2323 if (ulwp->ul_dead) { 2324 trs_flag = TRS_INVALID; 2325 } else if (!ulwp->ul_stop && !suspendedallmutators) { 2326 error = EINVAL; 2327 trs_flag = TRS_INVALID; 2328 } else if (ulwp->ul_stop) { 2329 trs_flag = TRS_NONVOLATILE; 2330 getgregs(ulwp, rs); 2331 } 2332 2333 if (flag) 2334 *flag = trs_flag; 2335 if (lwp) 2336 *lwp = tid; 2337 if (ss != NULL) 2338 (void) _thrp_stksegment(ulwp, ss); 2339 2340 ulwp_unlock(ulwp, udp); 2341 return (error); 2342 } 2343 2344 /* 2345 * Set the appropriate register state for the target thread. 2346 * This is not used by java. It exists solely for the MSTC test suite. 2347 */ 2348 #pragma weak thr_setstate = _thr_setstate 2349 int 2350 _thr_setstate(thread_t tid, int flag, gregset_t rs) 2351 { 2352 uberdata_t *udp = curthread->ul_uberdata; 2353 ulwp_t *ulwp; 2354 int error = 0; 2355 2356 if ((ulwp = find_lwp(tid)) == NULL) 2357 return (ESRCH); 2358 2359 if (!ulwp->ul_stop && !suspendedallmutators) 2360 error = EINVAL; 2361 else if (rs != NULL) { 2362 switch (flag) { 2363 case TRS_NONVOLATILE: 2364 /* do /proc stuff here? */ 2365 if (ulwp->ul_stop) 2366 setgregs(ulwp, rs); 2367 else 2368 error = EINVAL; 2369 break; 2370 case TRS_LWPID: /* do /proc stuff here? */ 2371 default: 2372 error = EINVAL; 2373 break; 2374 } 2375 } 2376 2377 ulwp_unlock(ulwp, udp); 2378 return (error); 2379 } 2380 2381 int 2382 getlwpstatus(thread_t tid, struct lwpstatus *sp) 2383 { 2384 extern ssize_t _pread(int, void *, size_t, off_t); 2385 char buf[100]; 2386 int fd; 2387 2388 /* "/proc/self/lwp/%u/lwpstatus" w/o stdio */ 2389 (void) strcpy(buf, "/proc/self/lwp/"); 2390 ultos((uint64_t)tid, 10, buf + strlen(buf)); 2391 (void) strcat(buf, "/lwpstatus"); 2392 if ((fd = _open(buf, O_RDONLY, 0)) >= 0) { 2393 while (_pread(fd, sp, sizeof (*sp), 0) == sizeof (*sp)) { 2394 if (sp->pr_flags & PR_STOPPED) { 2395 (void) _close(fd); 2396 return (0); 2397 } 2398 lwp_yield(); /* give him a chance to stop */ 2399 } 2400 (void) _close(fd); 2401 } 2402 return (-1); 2403 } 2404 2405 int 2406 putlwpregs(thread_t tid, prgregset_t prp) 2407 { 2408 extern ssize_t _writev(int, const struct iovec *, int); 2409 char buf[100]; 2410 int fd; 2411 long dstop_sreg[2]; 2412 long run_null[2]; 2413 iovec_t iov[3]; 2414 2415 /* "/proc/self/lwp/%u/lwpctl" w/o stdio */ 2416 (void) strcpy(buf, "/proc/self/lwp/"); 2417 ultos((uint64_t)tid, 10, buf + strlen(buf)); 2418 (void) strcat(buf, "/lwpctl"); 2419 if ((fd = _open(buf, O_WRONLY, 0)) >= 0) { 2420 dstop_sreg[0] = PCDSTOP; /* direct it to stop */ 2421 dstop_sreg[1] = PCSREG; /* set the registers */ 2422 iov[0].iov_base = (caddr_t)dstop_sreg; 2423 iov[0].iov_len = sizeof (dstop_sreg); 2424 iov[1].iov_base = (caddr_t)prp; /* from the register set */ 2425 iov[1].iov_len = sizeof (prgregset_t); 2426 run_null[0] = PCRUN; /* make it runnable again */ 2427 run_null[1] = 0; 2428 iov[2].iov_base = (caddr_t)run_null; 2429 iov[2].iov_len = sizeof (run_null); 2430 if (_writev(fd, iov, 3) >= 0) { 2431 (void) _close(fd); 2432 return (0); 2433 } 2434 (void) _close(fd); 2435 } 2436 return (-1); 2437 } 2438 2439 static ulong_t 2440 gettsp_slow(thread_t tid) 2441 { 2442 char buf[100]; 2443 struct lwpstatus status; 2444 2445 if (getlwpstatus(tid, &status) != 0) { 2446 /* "__gettsp(%u): can't read lwpstatus" w/o stdio */ 2447 (void) strcpy(buf, "__gettsp("); 2448 ultos((uint64_t)tid, 10, buf + strlen(buf)); 2449 (void) strcat(buf, "): can't read lwpstatus"); 2450 thr_panic(buf); 2451 } 2452 return (status.pr_reg[R_SP]); 2453 } 2454 2455 ulong_t 2456 __gettsp(thread_t tid) 2457 { 2458 uberdata_t *udp = curthread->ul_uberdata; 2459 ulwp_t *ulwp; 2460 ulong_t result; 2461 2462 if ((ulwp = find_lwp(tid)) == NULL) 2463 return (0); 2464 2465 if (ulwp->ul_stop && (result = ulwp->ul_sp) != 0) { 2466 ulwp_unlock(ulwp, udp); 2467 return (result); 2468 } 2469 2470 result = gettsp_slow(tid); 2471 ulwp_unlock(ulwp, udp); 2472 return (result); 2473 } 2474 2475 /* 2476 * This tells java stack walkers how to find the ucontext 2477 * structure passed to signal handlers. 2478 */ 2479 #pragma weak thr_sighndlrinfo = _thr_sighndlrinfo 2480 void 2481 _thr_sighndlrinfo(void (**func)(), int *funcsize) 2482 { 2483 *func = &__sighndlr; 2484 *funcsize = (char *)&__sighndlrend - (char *)&__sighndlr; 2485 } 2486 2487 /* 2488 * Mark a thread a mutator or reset a mutator to being a default, 2489 * non-mutator thread. 2490 */ 2491 #pragma weak thr_setmutator = _thr_setmutator 2492 int 2493 _thr_setmutator(thread_t tid, int enabled) 2494 { 2495 ulwp_t *self = curthread; 2496 uberdata_t *udp = self->ul_uberdata; 2497 ulwp_t *ulwp; 2498 int error; 2499 2500 enabled = enabled?1:0; 2501 top: 2502 if (tid == 0) { 2503 ulwp = self; 2504 ulwp_lock(ulwp, udp); 2505 } else if ((ulwp = find_lwp(tid)) == NULL) { 2506 return (ESRCH); 2507 } 2508 2509 /* 2510 * The target thread should be the caller itself or a suspended thread. 2511 * This prevents the target from also changing its ul_mutator field. 2512 */ 2513 error = 0; 2514 if (ulwp != self && !ulwp->ul_stop && enabled) 2515 error = EINVAL; 2516 else if (ulwp->ul_mutator != enabled) { 2517 lmutex_lock(&mutatorslock); 2518 if (mutatorsbarrier) { 2519 ulwp_unlock(ulwp, udp); 2520 while (mutatorsbarrier) 2521 (void) _cond_wait(&mutatorscv, &mutatorslock); 2522 lmutex_unlock(&mutatorslock); 2523 goto top; 2524 } 2525 ulwp->ul_mutator = enabled; 2526 lmutex_unlock(&mutatorslock); 2527 } 2528 2529 ulwp_unlock(ulwp, udp); 2530 return (error); 2531 } 2532 2533 /* 2534 * Establish a barrier against new mutators. Any non-mutator trying 2535 * to become a mutator is suspended until the barrier is removed. 2536 */ 2537 #pragma weak thr_mutators_barrier = _thr_mutators_barrier 2538 void 2539 _thr_mutators_barrier(int enabled) 2540 { 2541 int oldvalue; 2542 2543 lmutex_lock(&mutatorslock); 2544 2545 /* 2546 * Wait if trying to set the barrier while it is already set. 2547 */ 2548 while (mutatorsbarrier && enabled) 2549 (void) _cond_wait(&mutatorscv, &mutatorslock); 2550 2551 oldvalue = mutatorsbarrier; 2552 mutatorsbarrier = enabled; 2553 /* 2554 * Wakeup any blocked non-mutators when barrier is removed. 2555 */ 2556 if (oldvalue && !enabled) 2557 (void) cond_broadcast_internal(&mutatorscv); 2558 lmutex_unlock(&mutatorslock); 2559 } 2560 2561 /* 2562 * Suspend the set of all mutators except for the caller. The list 2563 * of actively running threads is searched and only the mutators 2564 * in this list are suspended. Actively running non-mutators remain 2565 * running. Any other thread is suspended. 2566 */ 2567 #pragma weak thr_suspend_allmutators = _thr_suspend_allmutators 2568 int 2569 _thr_suspend_allmutators(void) 2570 { 2571 ulwp_t *self = curthread; 2572 uberdata_t *udp = self->ul_uberdata; 2573 ulwp_t *ulwp; 2574 int link_dropped; 2575 2576 /* 2577 * We single-thread the entire thread suspend mechanism. 2578 */ 2579 (void) fork_lock_enter(NULL); 2580 top: 2581 lmutex_lock(&udp->link_lock); 2582 2583 if (suspendingallmutators || suspendedallmutators) { 2584 lmutex_unlock(&udp->link_lock); 2585 fork_lock_exit(); 2586 return (EINVAL); 2587 } 2588 suspendingallmutators = 1; 2589 2590 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) { 2591 ulwp_lock(ulwp, udp); 2592 if (!ulwp->ul_mutator) { 2593 ulwp_unlock(ulwp, udp); 2594 } else if (ulwp->ul_stop) { /* already stopped */ 2595 ulwp->ul_stop |= TSTP_MUTATOR; 2596 ulwp_broadcast(ulwp); 2597 ulwp_unlock(ulwp, udp); 2598 } else { 2599 /* 2600 * Move the stopped lwp out of a critical section. 2601 */ 2602 if (safe_suspend(ulwp, TSTP_MUTATOR, &link_dropped) || 2603 link_dropped) { 2604 suspendingallmutators = 0; 2605 goto top; 2606 } 2607 } 2608 } 2609 2610 suspendedallmutators = 1; 2611 suspendingallmutators = 0; 2612 lmutex_unlock(&udp->link_lock); 2613 fork_lock_exit(); 2614 return (0); 2615 } 2616 2617 /* 2618 * Suspend the target mutator. The caller is permitted to suspend 2619 * itself. If a mutator barrier is enabled, the caller will suspend 2620 * itself as though it had been suspended by thr_suspend_allmutators(). 2621 * When the barrier is removed, this thread will be resumed. Any 2622 * suspended mutator, whether suspended by thr_suspend_mutator(), or by 2623 * thr_suspend_allmutators(), can be resumed by thr_continue_mutator(). 2624 */ 2625 #pragma weak thr_suspend_mutator = _thr_suspend_mutator 2626 int 2627 _thr_suspend_mutator(thread_t tid) 2628 { 2629 if (tid == 0) 2630 tid = curthread->ul_lwpid; 2631 return (_thrp_suspend(tid, TSTP_MUTATOR)); 2632 } 2633 2634 /* 2635 * Resume the set of all suspended mutators. 2636 */ 2637 #pragma weak thr_continue_allmutators = _thr_continue_allmutators 2638 int 2639 _thr_continue_allmutators() 2640 { 2641 ulwp_t *self = curthread; 2642 uberdata_t *udp = self->ul_uberdata; 2643 ulwp_t *ulwp; 2644 2645 lmutex_lock(&udp->link_lock); 2646 if (!suspendedallmutators) { 2647 lmutex_unlock(&udp->link_lock); 2648 return (EINVAL); 2649 } 2650 suspendedallmutators = 0; 2651 2652 for (ulwp = self->ul_forw; ulwp != self; ulwp = ulwp->ul_forw) { 2653 mutex_t *mp = ulwp_mutex(ulwp, udp); 2654 lmutex_lock(mp); 2655 if (ulwp->ul_stop & TSTP_MUTATOR) { 2656 ulwp->ul_stop &= ~TSTP_MUTATOR; 2657 ulwp_broadcast(ulwp); 2658 if (!ulwp->ul_stop) 2659 force_continue(ulwp); 2660 } 2661 lmutex_unlock(mp); 2662 } 2663 2664 lmutex_unlock(&udp->link_lock); 2665 return (0); 2666 } 2667 2668 /* 2669 * Resume a suspended mutator. 2670 */ 2671 #pragma weak thr_continue_mutator = _thr_continue_mutator 2672 int 2673 _thr_continue_mutator(thread_t tid) 2674 { 2675 return (_thrp_continue(tid, TSTP_MUTATOR)); 2676 } 2677 2678 #pragma weak thr_wait_mutator = _thr_wait_mutator 2679 int 2680 _thr_wait_mutator(thread_t tid, int dontwait) 2681 { 2682 uberdata_t *udp = curthread->ul_uberdata; 2683 ulwp_t *ulwp; 2684 int error = 0; 2685 2686 top: 2687 if ((ulwp = find_lwp(tid)) == NULL) 2688 return (ESRCH); 2689 2690 if (!ulwp->ul_mutator) 2691 error = EINVAL; 2692 else if (dontwait) { 2693 if (!(ulwp->ul_stop & TSTP_MUTATOR)) 2694 error = EWOULDBLOCK; 2695 } else if (!(ulwp->ul_stop & TSTP_MUTATOR)) { 2696 cond_t *cvp = ulwp_condvar(ulwp, udp); 2697 mutex_t *mp = ulwp_mutex(ulwp, udp); 2698 2699 (void) _cond_wait(cvp, mp); 2700 (void) lmutex_unlock(mp); 2701 goto top; 2702 } 2703 2704 ulwp_unlock(ulwp, udp); 2705 return (error); 2706 } 2707 2708 /* PROBE_SUPPORT begin */ 2709 2710 void 2711 thr_probe_setup(void *data) 2712 { 2713 curthread->ul_tpdp = data; 2714 } 2715 2716 static void * 2717 _thread_probe_getfunc() 2718 { 2719 return (curthread->ul_tpdp); 2720 } 2721 2722 void * (*thr_probe_getfunc_addr)(void) = _thread_probe_getfunc; 2723 2724 /* ARGSUSED */ 2725 void 2726 _resume(ulwp_t *ulwp, caddr_t sp, int dontsave) 2727 { 2728 /* never called */ 2729 } 2730 2731 /* ARGSUSED */ 2732 void 2733 _resume_ret(ulwp_t *oldlwp) 2734 { 2735 /* never called */ 2736 } 2737 2738 /* PROBE_SUPPORT end */ 2739