1df57947fSPedro F. Giffuni /*- 2df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause 3df57947fSPedro F. Giffuni * 4a091d823SDavid Xu * Copyright (c) 2003 Daniel M. Eischen <deischen@freebsd.org> 5bb535300SJeff Roberson * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> 6bb535300SJeff Roberson * All rights reserved. 7bb535300SJeff Roberson * 8bb535300SJeff Roberson * Redistribution and use in source and binary forms, with or without 9bb535300SJeff Roberson * modification, are permitted provided that the following conditions 10bb535300SJeff Roberson * are met: 11bb535300SJeff Roberson * 1. Redistributions of source code must retain the above copyright 12bb535300SJeff Roberson * notice, this list of conditions and the following disclaimer. 13bb535300SJeff Roberson * 2. Redistributions in binary form must reproduce the above copyright 14bb535300SJeff Roberson * notice, this list of conditions and the following disclaimer in the 15bb535300SJeff Roberson * documentation and/or other materials provided with the distribution. 16bb535300SJeff Roberson * 3. All advertising materials mentioning features or use of this software 17bb535300SJeff Roberson * must display the following acknowledgement: 18bb535300SJeff Roberson * This product includes software developed by John Birrell. 19bb535300SJeff Roberson * 4. Neither the name of the author nor the names of any co-contributors 20bb535300SJeff Roberson * may be used to endorse or promote products derived from this software 21bb535300SJeff Roberson * without specific prior written permission. 22bb535300SJeff Roberson * 23bb535300SJeff Roberson * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 24bb535300SJeff Roberson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25bb535300SJeff Roberson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26bb535300SJeff Roberson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27bb535300SJeff Roberson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28bb535300SJeff Roberson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29bb535300SJeff Roberson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30bb535300SJeff Roberson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31bb535300SJeff Roberson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32bb535300SJeff Roberson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33bb535300SJeff Roberson * SUCH DAMAGE. 34bb535300SJeff Roberson */ 35bb535300SJeff Roberson 36bb535300SJeff Roberson #include "namespace.h" 37ebf7a015SKonstantin Belousov #include <sys/param.h> 38e2879eceSKonstantin Belousov #include <sys/auxv.h> 39a091d823SDavid Xu #include <sys/signalvar.h> 40bb535300SJeff Roberson #include <sys/ioctl.h> 418495e8b1SKonstantin Belousov #include <sys/link_elf.h> 426c8ce3bfSKonstantin Belousov #include <sys/resource.h> 43bb535300SJeff Roberson #include <sys/sysctl.h> 44bb535300SJeff Roberson #include <sys/ttycom.h> 45bb535300SJeff Roberson #include <sys/mman.h> 467b4f8f03SDavid Xu #include <sys/rtprio.h> 47bb535300SJeff Roberson #include <errno.h> 48bb535300SJeff Roberson #include <fcntl.h> 49bb535300SJeff Roberson #include <paths.h> 50bb535300SJeff Roberson #include <pthread.h> 51a091d823SDavid Xu #include <pthread_np.h> 52bb535300SJeff Roberson #include <signal.h> 53bb535300SJeff Roberson #include <stdlib.h> 54bb535300SJeff Roberson #include <string.h> 559572a734SDavid Xu #include <time.h> 56bb535300SJeff Roberson #include <unistd.h> 57bb535300SJeff Roberson #include "un-namespace.h" 58bb535300SJeff Roberson 59a091d823SDavid Xu #include "libc_private.h" 60bb535300SJeff Roberson #include "thr_private.h" 61bb535300SJeff Roberson 62f75b1ff6SMark Johnston char *_usrstack; 63cf905a15SDavid Xu struct pthread *_thr_initial; 64cf905a15SDavid Xu int _libthr_debug; 65cf905a15SDavid Xu int _thread_event_mask; 66cf905a15SDavid Xu struct pthread *_thread_last_event; 67cf905a15SDavid Xu pthreadlist _thread_list = TAILQ_HEAD_INITIALIZER(_thread_list); 68cf905a15SDavid Xu pthreadlist _thread_gc_list = TAILQ_HEAD_INITIALIZER(_thread_gc_list); 69cf905a15SDavid Xu int _thread_active_threads = 1; 70cf905a15SDavid Xu atfork_head _thr_atfork_list = TAILQ_HEAD_INITIALIZER(_thr_atfork_list); 71ada33a6eSDavid Xu struct urwlock _thr_atfork_lock = DEFAULT_URWLOCK; 72cf905a15SDavid Xu 73245116caSDavid Xu struct pthread_prio _thr_priorities[3] = { 747b4f8f03SDavid Xu {RTP_PRIO_MIN, RTP_PRIO_MAX, 0}, /* FIFO */ 757b4f8f03SDavid Xu {0, 0, 63}, /* OTHER */ 767b4f8f03SDavid Xu {RTP_PRIO_MIN, RTP_PRIO_MAX, 0} /* RR */ 77245116caSDavid Xu }; 78245116caSDavid Xu 79cf905a15SDavid Xu struct pthread_attr _pthread_attr_default = { 809ad4b644SDavid Xu .sched_policy = SCHED_OTHER, 8154dff16bSDavid Xu .sched_inherit = PTHREAD_INHERIT_SCHED, 82245116caSDavid Xu .prio = 0, 83cf905a15SDavid Xu .suspend = THR_CREATE_RUNNING, 846f79c826SDavid Xu .flags = PTHREAD_SCOPE_SYSTEM, 85cf905a15SDavid Xu .stackaddr_attr = NULL, 86cf905a15SDavid Xu .stacksize_attr = THR_STACK_DEFAULT, 87a759db94SDavid Xu .guardsize_attr = 0, 88a759db94SDavid Xu .cpusetsize = 0, 89a759db94SDavid Xu .cpuset = NULL 90cf905a15SDavid Xu }; 91cf905a15SDavid Xu 92cf905a15SDavid Xu struct pthread_mutex_attr _pthread_mutexattr_default = { 93cf905a15SDavid Xu .m_type = PTHREAD_MUTEX_DEFAULT, 94cf905a15SDavid Xu .m_protocol = PTHREAD_PRIO_NONE, 951bdbd705SKonstantin Belousov .m_ceiling = 0, 961bdbd705SKonstantin Belousov .m_pshared = PTHREAD_PROCESS_PRIVATE, 972a339d9eSKonstantin Belousov .m_robust = PTHREAD_MUTEX_STALLED, 98cf905a15SDavid Xu }; 99cf905a15SDavid Xu 100bbb64c21SDavid Xu struct pthread_mutex_attr _pthread_mutexattr_adaptive_default = { 101bbb64c21SDavid Xu .m_type = PTHREAD_MUTEX_ADAPTIVE_NP, 102bbb64c21SDavid Xu .m_protocol = PTHREAD_PRIO_NONE, 1031bdbd705SKonstantin Belousov .m_ceiling = 0, 1041bdbd705SKonstantin Belousov .m_pshared = PTHREAD_PROCESS_PRIVATE, 1052a339d9eSKonstantin Belousov .m_robust = PTHREAD_MUTEX_STALLED, 106bbb64c21SDavid Xu }; 107bbb64c21SDavid Xu 108cf905a15SDavid Xu /* Default condition variable attributes: */ 109cf905a15SDavid Xu struct pthread_cond_attr _pthread_condattr_default = { 110cf905a15SDavid Xu .c_pshared = PTHREAD_PROCESS_PRIVATE, 111cf905a15SDavid Xu .c_clockid = CLOCK_REALTIME 112cf905a15SDavid Xu }; 113cf905a15SDavid Xu 114d99f6dacSDavid Xu int _thr_is_smp = 0; 11537a6356bSDavid Xu size_t _thr_guard_default; 11637a6356bSDavid Xu size_t _thr_stack_default = THR_STACK_DEFAULT; 11737a6356bSDavid Xu size_t _thr_stack_initial = THR_STACK_INITIAL; 118cf905a15SDavid Xu int _thr_page_size; 1197416cdabSDavid Xu int _thr_spinloops; 1207416cdabSDavid Xu int _thr_yieldloops; 12184ac0fb8SDavid Xu int _thr_queuefifo = 4; 122cf905a15SDavid Xu int _gc_count; 123bddd24cdSDavid Xu struct umutex _mutex_static_lock = DEFAULT_UMUTEX; 124bddd24cdSDavid Xu struct umutex _cond_static_lock = DEFAULT_UMUTEX; 125bddd24cdSDavid Xu struct umutex _rwlock_static_lock = DEFAULT_UMUTEX; 126bddd24cdSDavid Xu struct umutex _keytable_lock = DEFAULT_UMUTEX; 127a9b764e2SDavid Xu struct urwlock _thr_list_lock = DEFAULT_URWLOCK; 128bddd24cdSDavid Xu struct umutex _thr_event_lock = DEFAULT_UMUTEX; 129a7b84c65SDavid Xu struct umutex _suspend_all_lock = DEFAULT_UMUTEX; 130a7b84c65SDavid Xu struct pthread *_single_thread; 131a7b84c65SDavid Xu int _suspend_all_cycle; 132a7b84c65SDavid Xu int _suspend_all_waiters; 133cf905a15SDavid Xu 134a091d823SDavid Xu int __pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *); 135a091d823SDavid Xu int __pthread_mutex_lock(pthread_mutex_t *); 136a091d823SDavid Xu int __pthread_mutex_trylock(pthread_mutex_t *); 137a091d823SDavid Xu void _thread_init_hack(void) __attribute__ ((constructor)); 138a091d823SDavid Xu 139a091d823SDavid Xu static void init_private(void); 140a091d823SDavid Xu static void init_main_thread(struct pthread *thread); 1414dee39feSMike Makonnen 142bb535300SJeff Roberson /* 143bb535300SJeff Roberson * All weak references used within libc should be in this table. 144a091d823SDavid Xu * This is so that static libraries will work. 145bb535300SJeff Roberson */ 14605c3a5eaSDavid Xu 14705c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_fork); 1489572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_getspecific); 1499572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_create); 1509572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_delete); 1519572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_destroy); 1529572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_init); 1539572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_lock); 1549572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_trylock); 1559572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_unlock); 1569572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutexattr_init); 1579572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutexattr_destroy); 1589572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutexattr_settype); 1599572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_once); 1609572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_setspecific); 16105c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_raise); 16205c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_destroy); 16305c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_getvalue); 16405c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_init); 16505c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_post); 16605c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_timedwait); 16705c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_trywait); 16805c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sem_wait); 1699572a734SDavid Xu STATIC_LIB_REQUIRE(_sigaction); 1709572a734SDavid Xu STATIC_LIB_REQUIRE(_sigprocmask); 1719572a734SDavid Xu STATIC_LIB_REQUIRE(_sigsuspend); 17205c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sigtimedwait); 17305c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sigwait); 17405c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_sigwaitinfo); 17505c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_spinlock); 17605c3a5eaSDavid Xu STATIC_LIB_REQUIRE(_spinunlock); 1779572a734SDavid Xu STATIC_LIB_REQUIRE(_thread_init_hack); 178bb535300SJeff Roberson 179bb535300SJeff Roberson /* 180bb535300SJeff Roberson * These are needed when linking statically. All references within 181bb535300SJeff Roberson * libgcc (and in the future libc) to these routines are weak, but 182bb535300SJeff Roberson * if they are not (strongly) referenced by the application or other 183bb535300SJeff Roberson * libraries, then the actual functions will not be loaded. 184bb535300SJeff Roberson */ 1859572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_once); 1869572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_create); 1879572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_key_delete); 1889572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_getspecific); 1899572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_setspecific); 1909572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_init); 1919572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_destroy); 1929572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_lock); 1939572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_trylock); 1949572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_mutex_unlock); 1959572a734SDavid Xu STATIC_LIB_REQUIRE(_pthread_create); 1969572a734SDavid Xu 1979572a734SDavid Xu /* Pull in all symbols required by libthread_db */ 1989572a734SDavid Xu STATIC_LIB_REQUIRE(_thread_state_running); 199bb535300SJeff Roberson 200a091d823SDavid Xu #define DUAL_ENTRY(entry) \ 201a091d823SDavid Xu (pthread_func_t)entry, (pthread_func_t)entry 202a091d823SDavid Xu 203a091d823SDavid Xu static pthread_func_t jmp_table[][2] = { 2040ab1bfc7SKonstantin Belousov [PJT_ATFORK] = {DUAL_ENTRY(_thr_atfork)}, 2050ab1bfc7SKonstantin Belousov [PJT_ATTR_DESTROY] = {DUAL_ENTRY(_thr_attr_destroy)}, 2060ab1bfc7SKonstantin Belousov [PJT_ATTR_GETDETACHSTATE] = {DUAL_ENTRY(_thr_attr_getdetachstate)}, 2070ab1bfc7SKonstantin Belousov [PJT_ATTR_GETGUARDSIZE] = {DUAL_ENTRY(_thr_attr_getguardsize)}, 2080ab1bfc7SKonstantin Belousov [PJT_ATTR_GETINHERITSCHED] = {DUAL_ENTRY(_thr_attr_getinheritsched)}, 2090ab1bfc7SKonstantin Belousov [PJT_ATTR_GETSCHEDPARAM] = {DUAL_ENTRY(_thr_attr_getschedparam)}, 2100ab1bfc7SKonstantin Belousov [PJT_ATTR_GETSCHEDPOLICY] = {DUAL_ENTRY(_thr_attr_getschedpolicy)}, 2110ab1bfc7SKonstantin Belousov [PJT_ATTR_GETSCOPE] = {DUAL_ENTRY(_thr_attr_getscope)}, 2120ab1bfc7SKonstantin Belousov [PJT_ATTR_GETSTACKADDR] = {DUAL_ENTRY(_thr_attr_getstackaddr)}, 2130ab1bfc7SKonstantin Belousov [PJT_ATTR_GETSTACKSIZE] = {DUAL_ENTRY(_thr_attr_getstacksize)}, 2140ab1bfc7SKonstantin Belousov [PJT_ATTR_INIT] = {DUAL_ENTRY(_thr_attr_init)}, 2150ab1bfc7SKonstantin Belousov [PJT_ATTR_SETDETACHSTATE] = {DUAL_ENTRY(_thr_attr_setdetachstate)}, 2160ab1bfc7SKonstantin Belousov [PJT_ATTR_SETGUARDSIZE] = {DUAL_ENTRY(_thr_attr_setguardsize)}, 2170ab1bfc7SKonstantin Belousov [PJT_ATTR_SETINHERITSCHED] = {DUAL_ENTRY(_thr_attr_setinheritsched)}, 2180ab1bfc7SKonstantin Belousov [PJT_ATTR_SETSCHEDPARAM] = {DUAL_ENTRY(_thr_attr_setschedparam)}, 2190ab1bfc7SKonstantin Belousov [PJT_ATTR_SETSCHEDPOLICY] = {DUAL_ENTRY(_thr_attr_setschedpolicy)}, 2200ab1bfc7SKonstantin Belousov [PJT_ATTR_SETSCOPE] = {DUAL_ENTRY(_thr_attr_setscope)}, 2210ab1bfc7SKonstantin Belousov [PJT_ATTR_SETSTACKADDR] = {DUAL_ENTRY(_thr_attr_setstackaddr)}, 2220ab1bfc7SKonstantin Belousov [PJT_ATTR_SETSTACKSIZE] = {DUAL_ENTRY(_thr_attr_setstacksize)}, 2230ab1bfc7SKonstantin Belousov [PJT_CANCEL] = {DUAL_ENTRY(_thr_cancel)}, 2240ab1bfc7SKonstantin Belousov [PJT_CLEANUP_POP] = {DUAL_ENTRY(_thr_cleanup_pop)}, 2250ab1bfc7SKonstantin Belousov [PJT_CLEANUP_PUSH] = {DUAL_ENTRY(_thr_cleanup_push)}, 2260ab1bfc7SKonstantin Belousov [PJT_COND_BROADCAST] = {DUAL_ENTRY(_thr_cond_broadcast)}, 2270ab1bfc7SKonstantin Belousov [PJT_COND_DESTROY] = {DUAL_ENTRY(_thr_cond_destroy)}, 2280ab1bfc7SKonstantin Belousov [PJT_COND_INIT] = {DUAL_ENTRY(_thr_cond_init)}, 2290ab1bfc7SKonstantin Belousov [PJT_COND_SIGNAL] = {DUAL_ENTRY(_thr_cond_signal)}, 2300ab1bfc7SKonstantin Belousov [PJT_COND_TIMEDWAIT] = {DUAL_ENTRY(_thr_cond_timedwait)}, 2310ab1bfc7SKonstantin Belousov [PJT_COND_WAIT] = {(pthread_func_t)__thr_cond_wait, 2320ab1bfc7SKonstantin Belousov (pthread_func_t)_thr_cond_wait}, 2330ab1bfc7SKonstantin Belousov [PJT_DETACH] = {DUAL_ENTRY(_thr_detach)}, 2340ab1bfc7SKonstantin Belousov [PJT_EQUAL] = {DUAL_ENTRY(_thr_equal)}, 2350ab1bfc7SKonstantin Belousov [PJT_EXIT] = {DUAL_ENTRY(_Tthr_exit)}, 2360ab1bfc7SKonstantin Belousov [PJT_GETSPECIFIC] = {DUAL_ENTRY(_thr_getspecific)}, 2370ab1bfc7SKonstantin Belousov [PJT_JOIN] = {DUAL_ENTRY(_thr_join)}, 2380ab1bfc7SKonstantin Belousov [PJT_KEY_CREATE] = {DUAL_ENTRY(_thr_key_create)}, 2390ab1bfc7SKonstantin Belousov [PJT_KEY_DELETE] = {DUAL_ENTRY(_thr_key_delete)}, 2400ab1bfc7SKonstantin Belousov [PJT_KILL] = {DUAL_ENTRY(_Tthr_kill)}, 2410ab1bfc7SKonstantin Belousov [PJT_MAIN_NP] = {DUAL_ENTRY(_thr_main_np)}, 2420ab1bfc7SKonstantin Belousov [PJT_MUTEXATTR_DESTROY] = {DUAL_ENTRY(_thr_mutexattr_destroy)}, 2430ab1bfc7SKonstantin Belousov [PJT_MUTEXATTR_INIT] = {DUAL_ENTRY(_thr_mutexattr_init)}, 2440ab1bfc7SKonstantin Belousov [PJT_MUTEXATTR_SETTYPE] = {DUAL_ENTRY(_thr_mutexattr_settype)}, 2450ab1bfc7SKonstantin Belousov [PJT_MUTEX_DESTROY] = {DUAL_ENTRY(_thr_mutex_destroy)}, 2460ab1bfc7SKonstantin Belousov [PJT_MUTEX_INIT] = {DUAL_ENTRY(__Tthr_mutex_init)}, 2470ab1bfc7SKonstantin Belousov [PJT_MUTEX_LOCK] = {DUAL_ENTRY(__Tthr_mutex_lock)}, 2480ab1bfc7SKonstantin Belousov [PJT_MUTEX_TRYLOCK] = {DUAL_ENTRY(__Tthr_mutex_trylock)}, 2490ab1bfc7SKonstantin Belousov [PJT_MUTEX_UNLOCK] = {DUAL_ENTRY(_thr_mutex_unlock)}, 2500ab1bfc7SKonstantin Belousov [PJT_ONCE] = {DUAL_ENTRY(_thr_once)}, 2510ab1bfc7SKonstantin Belousov [PJT_RWLOCK_DESTROY] = {DUAL_ENTRY(_thr_rwlock_destroy)}, 2520ab1bfc7SKonstantin Belousov [PJT_RWLOCK_INIT] = {DUAL_ENTRY(_thr_rwlock_init)}, 2530ab1bfc7SKonstantin Belousov [PJT_RWLOCK_RDLOCK] = {DUAL_ENTRY(_Tthr_rwlock_rdlock)}, 2540ab1bfc7SKonstantin Belousov [PJT_RWLOCK_TRYRDLOCK] = {DUAL_ENTRY(_Tthr_rwlock_tryrdlock)}, 2550ab1bfc7SKonstantin Belousov [PJT_RWLOCK_TRYWRLOCK] = {DUAL_ENTRY(_Tthr_rwlock_trywrlock)}, 2560ab1bfc7SKonstantin Belousov [PJT_RWLOCK_UNLOCK] = {DUAL_ENTRY(_Tthr_rwlock_unlock)}, 2570ab1bfc7SKonstantin Belousov [PJT_RWLOCK_WRLOCK] = {DUAL_ENTRY(_Tthr_rwlock_wrlock)}, 2580ab1bfc7SKonstantin Belousov [PJT_SELF] = {DUAL_ENTRY(_Tthr_self)}, 2590ab1bfc7SKonstantin Belousov [PJT_SETCANCELSTATE] = {DUAL_ENTRY(_thr_setcancelstate)}, 2600ab1bfc7SKonstantin Belousov [PJT_SETCANCELTYPE] = {DUAL_ENTRY(_thr_setcanceltype)}, 2610ab1bfc7SKonstantin Belousov [PJT_SETSPECIFIC] = {DUAL_ENTRY(_thr_setspecific)}, 2620ab1bfc7SKonstantin Belousov [PJT_SIGMASK] = {DUAL_ENTRY(_thr_sigmask)}, 2630ab1bfc7SKonstantin Belousov [PJT_TESTCANCEL] = {DUAL_ENTRY(_Tthr_testcancel)}, 2640ab1bfc7SKonstantin Belousov [PJT_CLEANUP_POP_IMP] = {DUAL_ENTRY(__thr_cleanup_pop_imp)}, 2650ab1bfc7SKonstantin Belousov [PJT_CLEANUP_PUSH_IMP] = {DUAL_ENTRY(__thr_cleanup_push_imp)}, 26665174f68SKonstantin Belousov [PJT_CANCEL_ENTER] = {DUAL_ENTRY(_Tthr_cancel_enter)}, 26765174f68SKonstantin Belousov [PJT_CANCEL_LEAVE] = {DUAL_ENTRY(_Tthr_cancel_leave)}, 2680ab1bfc7SKonstantin Belousov [PJT_MUTEX_CONSISTENT] = {DUAL_ENTRY(_Tthr_mutex_consistent)}, 2690ab1bfc7SKonstantin Belousov [PJT_MUTEXATTR_GETROBUST] = {DUAL_ENTRY(_thr_mutexattr_getrobust)}, 2700ab1bfc7SKonstantin Belousov [PJT_MUTEXATTR_SETROBUST] = {DUAL_ENTRY(_thr_mutexattr_setrobust)}, 2710ab1bfc7SKonstantin Belousov [PJT_GETTHREADID_NP] = {DUAL_ENTRY(_thr_getthreadid_np)}, 272412ef5daSKonstantin Belousov [PJT_ATTR_GET_NP] = {DUAL_ENTRY(_thr_attr_get_np)}, 2730dc52b72SMinsoo Choo [PJT_GETNAME_NP] = {DUAL_ENTRY(_thr_getname_np)}, 27483aafcdcSKyle Evans [PJT_SUSPEND_ALL_NP] = {DUAL_ENTRY(_thr_suspend_all_np)}, 27583aafcdcSKyle Evans [PJT_RESUME_ALL_NP] = {DUAL_ENTRY(_thr_resume_all_np)}, 276a091d823SDavid Xu }; 277a091d823SDavid Xu 278a091d823SDavid Xu static int init_once = 0; 279bb535300SJeff Roberson 280bb535300SJeff Roberson /* 281a091d823SDavid Xu * For the shared version of the threads library, the above is sufficient. 282a091d823SDavid Xu * But for the archive version of the library, we need a little bit more. 283a091d823SDavid Xu * Namely, we must arrange for this particular module to be pulled in from 284a091d823SDavid Xu * the archive library at link time. To accomplish that, we define and 285a091d823SDavid Xu * initialize a variable, "_thread_autoinit_dummy_decl". This variable is 286a091d823SDavid Xu * referenced (as an extern) from libc/stdlib/exit.c. This will always 287a091d823SDavid Xu * create a need for this module, ensuring that it is present in the 288a091d823SDavid Xu * executable. 289a091d823SDavid Xu */ 290a091d823SDavid Xu extern int _thread_autoinit_dummy_decl; 291a091d823SDavid Xu int _thread_autoinit_dummy_decl = 0; 292a091d823SDavid Xu 293a091d823SDavid Xu void 294a091d823SDavid Xu _thread_init_hack(void) 295a091d823SDavid Xu { 296a091d823SDavid Xu 297a091d823SDavid Xu _libpthread_init(NULL); 298a091d823SDavid Xu } 299a091d823SDavid Xu 300a091d823SDavid Xu 301a091d823SDavid Xu /* 302a091d823SDavid Xu * Threaded process initialization. 303a091d823SDavid Xu * 304a091d823SDavid Xu * This is only called under two conditions: 305a091d823SDavid Xu * 306a091d823SDavid Xu * 1) Some thread routines have detected that the library hasn't yet 307a091d823SDavid Xu * been initialized (_thr_initial == NULL && curthread == NULL), or 308a091d823SDavid Xu * 309a091d823SDavid Xu * 2) An explicit call to reinitialize after a fork (indicated 310a091d823SDavid Xu * by curthread != NULL) 311f2c3dd08SMike Makonnen */ 312f2c3dd08SMike Makonnen void 313a091d823SDavid Xu _libpthread_init(struct pthread *curthread) 314f2c3dd08SMike Makonnen { 315b6751c3fSKonstantin Belousov int first, dlopened; 316bb535300SJeff Roberson 317bb535300SJeff Roberson /* Check if this function has already been called: */ 3182a339d9eSKonstantin Belousov if (_thr_initial != NULL && curthread == NULL) 319a091d823SDavid Xu /* Only initialize the threaded application once. */ 320bb535300SJeff Roberson return; 321bb535300SJeff Roberson 322bb535300SJeff Roberson /* 323a091d823SDavid Xu * Check the size of the jump table to make sure it is preset 324a091d823SDavid Xu * with the correct number of entries. 325a091d823SDavid Xu */ 3262a339d9eSKonstantin Belousov if (sizeof(jmp_table) != sizeof(pthread_func_t) * PJT_MAX * 2) 327a091d823SDavid Xu PANIC("Thread jump table not properly initialized"); 328a091d823SDavid Xu memcpy(__thr_jtable, jmp_table, sizeof(jmp_table)); 3298495e8b1SKonstantin Belousov __thr_interpose_libc(); 330a091d823SDavid Xu 331a091d823SDavid Xu /* Initialize pthread private data. */ 332a091d823SDavid Xu init_private(); 333a091d823SDavid Xu 334a091d823SDavid Xu /* Set the initial thread. */ 335a091d823SDavid Xu if (curthread == NULL) { 336a091d823SDavid Xu first = 1; 337*676be271SKonstantin Belousov /* Force _get_curthread() return NULL until set. */ 338*676be271SKonstantin Belousov _tcb_get()->tcb_thread = NULL; 339a091d823SDavid Xu /* Create and initialize the initial thread. */ 340a091d823SDavid Xu curthread = _thr_alloc(NULL); 341a091d823SDavid Xu if (curthread == NULL) 342a091d823SDavid Xu PANIC("Can't allocate initial thread"); 343a091d823SDavid Xu init_main_thread(curthread); 3448495e8b1SKonstantin Belousov } else { 3458495e8b1SKonstantin Belousov first = 0; 346a091d823SDavid Xu } 3478495e8b1SKonstantin Belousov 348bb535300SJeff Roberson /* 349a091d823SDavid Xu * Add the thread to the thread list queue. 350bb535300SJeff Roberson */ 351a091d823SDavid Xu THR_LIST_ADD(curthread); 352a091d823SDavid Xu _thread_active_threads = 1; 353a091d823SDavid Xu 354a091d823SDavid Xu /* Setup the thread specific data */ 355a091d823SDavid Xu _tcb_set(curthread->tcb); 356a091d823SDavid Xu 357a091d823SDavid Xu if (first) { 358a091d823SDavid Xu _thr_initial = curthread; 3598495e8b1SKonstantin Belousov dlopened = _rtld_is_dlopened(&_thread_autoinit_dummy_decl) != 0; 3608495e8b1SKonstantin Belousov _thr_signal_init(dlopened); 3617a4cd8d3SDavid Xu if (_thread_event_mask & TD_CREATE) 362d245d9e1SDavid Xu _thr_report_creation(curthread, curthread); 363b18943f3SJilles Tjoelker /* 364b18943f3SJilles Tjoelker * Always use our rtld lock implementation. 365b18943f3SJilles Tjoelker * It is faster because it postpones signal handlers 366b18943f3SJilles Tjoelker * instead of calling sigprocmask(2). 367b18943f3SJilles Tjoelker */ 368b18943f3SJilles Tjoelker _thr_rtld_init(); 369a091d823SDavid Xu } 370bb535300SJeff Roberson } 3713f07b4bcSMike Makonnen 372a091d823SDavid Xu /* 373a091d823SDavid Xu * This function and pthread_create() do a lot of the same things. 374a091d823SDavid Xu * It'd be nice to consolidate the common stuff in one place. 375a091d823SDavid Xu */ 376a091d823SDavid Xu static void 377a091d823SDavid Xu init_main_thread(struct pthread *thread) 378a091d823SDavid Xu { 379e2dc286cSDavid Xu struct sched_param sched_param; 3801bdbd705SKonstantin Belousov int i; 381e2dc286cSDavid Xu 382a091d823SDavid Xu /* Setup the thread attributes. */ 383a091d823SDavid Xu thr_self(&thread->tid); 384a091d823SDavid Xu thread->attr = _pthread_attr_default; 385a091d823SDavid Xu /* 386a091d823SDavid Xu * Set up the thread stack. 387a091d823SDavid Xu * 388a091d823SDavid Xu * Create a red zone below the main stack. All other stacks 389a091d823SDavid Xu * are constrained to a maximum size by the parameters 390a091d823SDavid Xu * passed to mmap(), but this stack is only limited by 391a091d823SDavid Xu * resource limits, so this stack needs an explicitly mapped 392a091d823SDavid Xu * red zone to protect the thread stack that is just beyond. 393a091d823SDavid Xu */ 394f75b1ff6SMark Johnston if (mmap(_usrstack - _thr_stack_initial - 395a091d823SDavid Xu _thr_guard_default, _thr_guard_default, 0, MAP_ANON, 396a091d823SDavid Xu -1, 0) == MAP_FAILED) 397a091d823SDavid Xu PANIC("Cannot allocate red zone for initial thread"); 398bb535300SJeff Roberson 399a091d823SDavid Xu /* 400a091d823SDavid Xu * Mark the stack as an application supplied stack so that it 401a091d823SDavid Xu * isn't deallocated. 402a091d823SDavid Xu * 403a091d823SDavid Xu * XXX - I'm not sure it would hurt anything to deallocate 404a091d823SDavid Xu * the main thread stack because deallocation doesn't 405a091d823SDavid Xu * actually free() it; it just puts it in the free 406a091d823SDavid Xu * stack queue for later reuse. 407a091d823SDavid Xu */ 408f75b1ff6SMark Johnston thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial; 409a091d823SDavid Xu thread->attr.stacksize_attr = _thr_stack_initial; 410a091d823SDavid Xu thread->attr.guardsize_attr = _thr_guard_default; 411a091d823SDavid Xu thread->attr.flags |= THR_STACK_USER; 412bb535300SJeff Roberson 413a091d823SDavid Xu /* 414a091d823SDavid Xu * Write a magic value to the thread structure 415a091d823SDavid Xu * to help identify valid ones: 416a091d823SDavid Xu */ 417a091d823SDavid Xu thread->magic = THR_MAGIC; 418a091d823SDavid Xu 419f08e1bf6SDavid Xu thread->cancel_enable = 1; 420f08e1bf6SDavid Xu thread->cancel_async = 0; 421a091d823SDavid Xu 4221bdbd705SKonstantin Belousov /* Initialize the mutex queues */ 4231bdbd705SKonstantin Belousov for (i = 0; i < TMQ_NITEMS; i++) 4241bdbd705SKonstantin Belousov TAILQ_INIT(&thread->mq[i]); 425a091d823SDavid Xu 426a091d823SDavid Xu thread->state = PS_RUNNING; 427a091d823SDavid Xu 428e6747c7cSDavid Xu _thr_getscheduler(thread->tid, &thread->attr.sched_policy, 429e6747c7cSDavid Xu &sched_param); 430e2dc286cSDavid Xu thread->attr.prio = sched_param.sched_priority; 431e2dc286cSDavid Xu 4323832fd24SDavid Xu #ifdef _PTHREAD_FORCED_UNWIND 433f75b1ff6SMark Johnston thread->unwind_stackend = _usrstack; 4343832fd24SDavid Xu #endif 4353832fd24SDavid Xu 436a091d823SDavid Xu /* Others cleared to zero by thr_alloc() */ 437a091d823SDavid Xu } 438a091d823SDavid Xu 439e03c7f50SKonstantin Belousov bool 440e03c7f50SKonstantin Belousov __thr_get_main_stack_base(char **base) 441e03c7f50SKonstantin Belousov { 442e03c7f50SKonstantin Belousov size_t len; 443e03c7f50SKonstantin Belousov int mib[2]; 444e03c7f50SKonstantin Belousov 445e03c7f50SKonstantin Belousov if (elf_aux_info(AT_USRSTACKBASE, base, sizeof(*base)) == 0) 446e03c7f50SKonstantin Belousov return (true); 447e03c7f50SKonstantin Belousov 448e03c7f50SKonstantin Belousov mib[0] = CTL_KERN; 449e03c7f50SKonstantin Belousov mib[1] = KERN_USRSTACK; 450e03c7f50SKonstantin Belousov len = sizeof(*base); 451e03c7f50SKonstantin Belousov if (sysctl(mib, nitems(mib), base, &len, NULL, 0) == 0) 452e03c7f50SKonstantin Belousov return (true); 453e03c7f50SKonstantin Belousov 454e03c7f50SKonstantin Belousov return (false); 455e03c7f50SKonstantin Belousov } 456e03c7f50SKonstantin Belousov 457e03c7f50SKonstantin Belousov bool 458e03c7f50SKonstantin Belousov __thr_get_main_stack_lim(size_t *lim) 459e03c7f50SKonstantin Belousov { 460e03c7f50SKonstantin Belousov struct rlimit rlim; 461e03c7f50SKonstantin Belousov 462e03c7f50SKonstantin Belousov if (elf_aux_info(AT_USRSTACKLIM, lim, sizeof(*lim)) == 0) 463e03c7f50SKonstantin Belousov return (true); 464e03c7f50SKonstantin Belousov 465e03c7f50SKonstantin Belousov if (getrlimit(RLIMIT_STACK, &rlim) == 0) { 466e03c7f50SKonstantin Belousov *lim = rlim.rlim_cur; 467e03c7f50SKonstantin Belousov return (true); 468e03c7f50SKonstantin Belousov } 469e03c7f50SKonstantin Belousov 470e03c7f50SKonstantin Belousov return (false); 471e03c7f50SKonstantin Belousov } 472e03c7f50SKonstantin Belousov 473a091d823SDavid Xu static void 474a091d823SDavid Xu init_private(void) 475a091d823SDavid Xu { 47636bcb07aSKonstantin Belousov char *env, *env_bigstack, *env_splitstack; 47774c75113SDavid Xu 478bddd24cdSDavid Xu _thr_umutex_init(&_mutex_static_lock); 479bddd24cdSDavid Xu _thr_umutex_init(&_cond_static_lock); 480bddd24cdSDavid Xu _thr_umutex_init(&_rwlock_static_lock); 481bddd24cdSDavid Xu _thr_umutex_init(&_keytable_lock); 482ada33a6eSDavid Xu _thr_urwlock_init(&_thr_atfork_lock); 483bddd24cdSDavid Xu _thr_umutex_init(&_thr_event_lock); 484a7b84c65SDavid Xu _thr_umutex_init(&_suspend_all_lock); 485a091d823SDavid Xu _thr_spinlock_init(); 486a091d823SDavid Xu _thr_list_init(); 487d1078b0bSDavid Xu _thr_wake_addr_init(); 488d1078b0bSDavid Xu _sleepq_init(); 489a7b84c65SDavid Xu _single_thread = NULL; 490a7b84c65SDavid Xu _suspend_all_waiters = 0; 491a091d823SDavid Xu 492a091d823SDavid Xu /* 493a091d823SDavid Xu * Avoid reinitializing some things if they don't need to be, 494a091d823SDavid Xu * e.g. after a fork(). 495a091d823SDavid Xu */ 496a091d823SDavid Xu if (init_once == 0) { 49753fd961fSKonstantin Belousov __thr_pshared_init(); 498381c2d2eSKonstantin Belousov __thr_malloc_init(); 499e2879eceSKonstantin Belousov 500bb535300SJeff Roberson /* Find the stack top */ 501e03c7f50SKonstantin Belousov if (!__thr_get_main_stack_base(&_usrstack)) 502e03c7f50SKonstantin Belousov PANIC("Cannot get kern.usrstack"); 50336bcb07aSKonstantin Belousov env_bigstack = getenv("LIBPTHREAD_BIGSTACK_MAIN"); 50436bcb07aSKonstantin Belousov env_splitstack = getenv("LIBPTHREAD_SPLITSTACK_MAIN"); 50536bcb07aSKonstantin Belousov if (env_bigstack != NULL || env_splitstack == NULL) { 506e03c7f50SKonstantin Belousov if (!__thr_get_main_stack_lim(&_thr_stack_initial)) 5076c8ce3bfSKonstantin Belousov PANIC("Cannot get stack rlimit"); 508e2879eceSKonstantin Belousov } 509a4f02d5dSEdward Tomasz Napierala _thr_is_smp = sysconf(_SC_NPROCESSORS_CONF); 510a4f02d5dSEdward Tomasz Napierala if (_thr_is_smp == -1) 511a4f02d5dSEdward Tomasz Napierala PANIC("Cannot get _SC_NPROCESSORS_CONF"); 512d99f6dacSDavid Xu _thr_is_smp = (_thr_is_smp > 1); 513a091d823SDavid Xu _thr_page_size = getpagesize(); 514a091d823SDavid Xu _thr_guard_default = _thr_page_size; 515a091d823SDavid Xu _pthread_attr_default.guardsize_attr = _thr_guard_default; 516a091d823SDavid Xu _pthread_attr_default.stacksize_attr = _thr_stack_default; 5177416cdabSDavid Xu env = getenv("LIBPTHREAD_SPINLOOPS"); 5187416cdabSDavid Xu if (env) 5197416cdabSDavid Xu _thr_spinloops = atoi(env); 5207416cdabSDavid Xu env = getenv("LIBPTHREAD_YIELDLOOPS"); 5217416cdabSDavid Xu if (env) 5227416cdabSDavid Xu _thr_yieldloops = atoi(env); 52384ac0fb8SDavid Xu env = getenv("LIBPTHREAD_QUEUE_FIFO"); 52484ac0fb8SDavid Xu if (env) 52584ac0fb8SDavid Xu _thr_queuefifo = atoi(env); 52693ca6ff2SKonstantin Belousov env = getenv("LIBPTHREAD_UMTX_MIN_TIMEOUT"); 52793ca6ff2SKonstantin Belousov if (env) { 52893ca6ff2SKonstantin Belousov char *endptr; 52993ca6ff2SKonstantin Belousov long mint; 53093ca6ff2SKonstantin Belousov 53193ca6ff2SKonstantin Belousov mint = strtol(env, &endptr, 0); 53293ca6ff2SKonstantin Belousov if (*endptr == '\0' && mint >= 0) { 53393ca6ff2SKonstantin Belousov _umtx_op(NULL, UMTX_OP_SET_MIN_TIMEOUT, 53493ca6ff2SKonstantin Belousov mint, NULL, NULL); 53593ca6ff2SKonstantin Belousov } 53693ca6ff2SKonstantin Belousov } 537a091d823SDavid Xu } 538a091d823SDavid Xu init_once = 1; 539a091d823SDavid Xu } 540