init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* Copyright (C) 2002-2007, 2008, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <limits.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/param.h>
  22. #include <sys/resource.h>
  23. #include <pthreadP.h>
  24. #include <atomic.h>
  25. #include <ldsodefs.h>
  26. #include <tls.h>
  27. #include <fork.h>
  28. #include <version.h>
  29. #include <smp.h>
  30. #include <lowlevellock.h>
  31. #include <bits/kernel-features.h>
  32. #include <stdio.h>
  33. /* Size and alignment of static TLS block. */
  34. size_t __static_tls_size;
  35. size_t __static_tls_align_m1;
  36. #ifndef __ASSUME_SET_ROBUST_LIST
  37. /* Negative if we do not have the system call and we can use it. */
  38. int __set_robust_list_avail;
  39. # define set_robust_list_not_avail() \
  40. __set_robust_list_avail = -1
  41. #else
  42. # define set_robust_list_not_avail() do { } while (0)
  43. #endif
  44. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  45. /* Nonzero if we do not have FUTEX_CLOCK_REALTIME. */
  46. int __have_futex_clock_realtime;
  47. # define __set_futex_clock_realtime() \
  48. __have_futex_clock_realtime = 1
  49. #else
  50. #define __set_futex_clock_realtime() do { } while (0)
  51. #endif
  52. /* Version of the library, used in libthread_db to detect mismatches. */
  53. static const char nptl_version[] __attribute_used__ = VERSION;
  54. #ifdef SHARED
  55. static void nptl_freeres (void);
  56. static const struct pthread_functions pthread_functions =
  57. {
  58. .ptr_pthread_attr_destroy = __pthread_attr_destroy,
  59. .ptr___pthread_attr_init_2_1 = __pthread_attr_init_2_1,
  60. .ptr_pthread_attr_getdetachstate = __pthread_attr_getdetachstate,
  61. .ptr_pthread_attr_setdetachstate = __pthread_attr_setdetachstate,
  62. .ptr_pthread_attr_getinheritsched = __pthread_attr_getinheritsched,
  63. .ptr_pthread_attr_setinheritsched = __pthread_attr_setinheritsched,
  64. .ptr_pthread_attr_getschedparam = __pthread_attr_getschedparam,
  65. .ptr_pthread_attr_setschedparam = __pthread_attr_setschedparam,
  66. .ptr_pthread_attr_getschedpolicy = __pthread_attr_getschedpolicy,
  67. .ptr_pthread_attr_setschedpolicy = __pthread_attr_setschedpolicy,
  68. .ptr_pthread_attr_getscope = __pthread_attr_getscope,
  69. .ptr_pthread_attr_setscope = __pthread_attr_setscope,
  70. .ptr_pthread_condattr_destroy = __pthread_condattr_destroy,
  71. .ptr_pthread_condattr_init = __pthread_condattr_init,
  72. .ptr___pthread_cond_broadcast = __pthread_cond_broadcast,
  73. .ptr___pthread_cond_destroy = __pthread_cond_destroy,
  74. .ptr___pthread_cond_init = __pthread_cond_init,
  75. .ptr___pthread_cond_signal = __pthread_cond_signal,
  76. .ptr___pthread_cond_wait = __pthread_cond_wait,
  77. .ptr___pthread_cond_timedwait = __pthread_cond_timedwait,
  78. .ptr_pthread_equal = __pthread_equal,
  79. .ptr___pthread_exit = __pthread_exit,
  80. .ptr_pthread_getschedparam = __pthread_getschedparam,
  81. .ptr_pthread_setschedparam = __pthread_setschedparam,
  82. .ptr_pthread_mutex_destroy = INTUSE(__pthread_mutex_destroy),
  83. .ptr_pthread_mutex_init = INTUSE(__pthread_mutex_init),
  84. .ptr_pthread_mutex_lock = INTUSE(__pthread_mutex_lock),
  85. .ptr_pthread_mutex_unlock = INTUSE(__pthread_mutex_unlock),
  86. .ptr_pthread_self = __pthread_self,
  87. .ptr_pthread_setcancelstate = __pthread_setcancelstate,
  88. .ptr_pthread_setcanceltype = __pthread_setcanceltype,
  89. .ptr___pthread_cleanup_upto = __pthread_cleanup_upto,
  90. .ptr___pthread_once = __pthread_once_internal,
  91. .ptr___pthread_rwlock_rdlock = __pthread_rwlock_rdlock_internal,
  92. .ptr___pthread_rwlock_wrlock = __pthread_rwlock_wrlock_internal,
  93. .ptr___pthread_rwlock_unlock = __pthread_rwlock_unlock_internal,
  94. .ptr___pthread_key_create = __pthread_key_create_internal,
  95. .ptr___pthread_getspecific = __pthread_getspecific_internal,
  96. .ptr___pthread_setspecific = __pthread_setspecific_internal,
  97. .ptr__pthread_cleanup_push_defer = _pthread_cleanup_push_defer,
  98. .ptr__pthread_cleanup_pop_restore = _pthread_cleanup_pop_restore,
  99. .ptr_nthreads = &__nptl_nthreads,
  100. .ptr___pthread_unwind = &__pthread_unwind,
  101. .ptr__nptl_deallocate_tsd = __nptl_deallocate_tsd,
  102. .ptr__nptl_setxid = __nptl_setxid,
  103. /* For now only the stack cache needs to be freed. */
  104. .ptr_freeres = nptl_freeres
  105. };
  106. # define ptr_pthread_functions &pthread_functions
  107. #else
  108. # define ptr_pthread_functions NULL
  109. #endif
  110. #ifdef SHARED
  111. /* This function is called indirectly from the freeres code in libc. */
  112. static void
  113. __libc_freeres_fn_section
  114. nptl_freeres (void)
  115. {
  116. __unwind_freeres ();
  117. __free_stacks (0);
  118. }
  119. #endif
  120. /* For asynchronous cancellation we use a signal. This is the handler. */
  121. static void
  122. sigcancel_handler (int sig, siginfo_t *si, void *ctx)
  123. {
  124. #ifdef __ASSUME_CORRECT_SI_PID
  125. /* Determine the process ID. It might be negative if the thread is
  126. in the middle of a fork() call. */
  127. pid_t pid = THREAD_GETMEM (THREAD_SELF, pid);
  128. if (__builtin_expect (pid < 0, 0))
  129. pid = -pid;
  130. #endif
  131. /* Safety check. It would be possible to call this function for
  132. other signals and send a signal from another process. This is not
  133. correct and might even be a security problem. Try to catch as
  134. many incorrect invocations as possible. */
  135. if (sig != SIGCANCEL
  136. #ifdef __ASSUME_CORRECT_SI_PID
  137. /* Kernels before 2.5.75 stored the thread ID and not the process
  138. ID in si_pid so we skip this test. */
  139. || si->si_pid != pid
  140. #endif
  141. || si->si_code != SI_TKILL)
  142. return;
  143. struct pthread *self = THREAD_SELF;
  144. int oldval = THREAD_GETMEM (self, cancelhandling);
  145. while (1)
  146. {
  147. /* We are canceled now. When canceled by another thread this flag
  148. is already set but if the signal is directly send (internally or
  149. from another process) is has to be done here. */
  150. int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
  151. if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
  152. /* Already canceled or exiting. */
  153. break;
  154. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
  155. oldval);
  156. if (curval == oldval)
  157. {
  158. /* Set the return value. */
  159. THREAD_SETMEM (self, result, PTHREAD_CANCELED);
  160. /* Make sure asynchronous cancellation is still enabled. */
  161. if ((newval & CANCELTYPE_BITMASK) != 0)
  162. /* Run the registered destructors and terminate the thread. */
  163. __do_cancel ();
  164. break;
  165. }
  166. oldval = curval;
  167. }
  168. }
  169. struct xid_command *__xidcmd attribute_hidden;
  170. /* For asynchronous cancellation we use a signal. This is the handler. */
  171. static void
  172. sighandler_setxid (int sig, siginfo_t *si, void *ctx)
  173. {
  174. #ifdef __ASSUME_CORRECT_SI_PID
  175. /* Determine the process ID. It might be negative if the thread is
  176. in the middle of a fork() call. */
  177. pid_t pid = THREAD_GETMEM (THREAD_SELF, pid);
  178. if (__builtin_expect (pid < 0, 0))
  179. pid = -pid;
  180. #endif
  181. /* Safety check. It would be possible to call this function for
  182. other signals and send a signal from another process. This is not
  183. correct and might even be a security problem. Try to catch as
  184. many incorrect invocations as possible. */
  185. if (sig != SIGSETXID
  186. #ifdef __ASSUME_CORRECT_SI_PID
  187. /* Kernels before 2.5.75 stored the thread ID and not the process
  188. ID in si_pid so we skip this test. */
  189. || si->si_pid != pid
  190. #endif
  191. || si->si_code != SI_TKILL)
  192. return;
  193. INTERNAL_SYSCALL_DECL (err);
  194. INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, err, 3, __xidcmd->id[0],
  195. __xidcmd->id[1], __xidcmd->id[2]);
  196. /* Reset the SETXID flag. */
  197. struct pthread *self = THREAD_SELF;
  198. int flags, newval;
  199. do
  200. {
  201. flags = THREAD_GETMEM (self, cancelhandling);
  202. newval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
  203. flags & ~SETXID_BITMASK, flags);
  204. }
  205. while (flags != newval);
  206. /* And release the futex. */
  207. self->setxid_futex = 1;
  208. lll_futex_wake (&self->setxid_futex, 1, LLL_PRIVATE);
  209. if (atomic_decrement_val (&__xidcmd->cntr) == 0)
  210. lll_futex_wake (&__xidcmd->cntr, 1, LLL_PRIVATE);
  211. }
  212. /* When using __thread for this, we do it in libc so as not
  213. to give libpthread its own TLS segment just for this. */
  214. extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
  215. /* This can be set by the debugger before initialization is complete. */
  216. static bool __nptl_initial_report_events __attribute_used__;
  217. void __pthread_initialize_minimal_internal (void) attribute_hidden;
  218. void
  219. __pthread_initialize_minimal_internal (void)
  220. {
  221. static int initialized = 0;
  222. if (initialized)
  223. return;
  224. initialized = 1;
  225. /* Minimal initialization of the thread descriptor. */
  226. struct pthread *pd = THREAD_SELF;
  227. INTERNAL_SYSCALL_DECL (err);
  228. pd->pid = pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid);
  229. THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
  230. THREAD_SETMEM (pd, user_stack, true);
  231. if (LLL_LOCK_INITIALIZER != 0)
  232. THREAD_SETMEM (pd, lock, LLL_LOCK_INITIALIZER);
  233. #if HP_TIMING_AVAIL
  234. THREAD_SETMEM (pd, cpuclock_offset, GL(dl_cpuclock_offset));
  235. #endif
  236. /* Initialize the robust mutex data. */
  237. #ifdef __PTHREAD_MUTEX_HAVE_PREV
  238. pd->robust_prev = &pd->robust_head;
  239. #endif
  240. pd->robust_head.list = &pd->robust_head;
  241. #ifdef __NR_set_robust_list
  242. pd->robust_head.futex_offset = (offsetof (pthread_mutex_t, __data.__lock)
  243. - offsetof (pthread_mutex_t,
  244. __data.__list.__next));
  245. int res = INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
  246. sizeof (struct robust_list_head));
  247. if (INTERNAL_SYSCALL_ERROR_P (res, err))
  248. #endif
  249. set_robust_list_not_avail ();
  250. #ifndef __ASSUME_PRIVATE_FUTEX
  251. /* Private futexes are always used (at least internally) so that
  252. doing the test once this early is beneficial. */
  253. {
  254. int word = 0;
  255. word = INTERNAL_SYSCALL (futex, err, 3, &word,
  256. FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
  257. if (!INTERNAL_SYSCALL_ERROR_P (word, err))
  258. THREAD_SETMEM (pd, header.private_futex, FUTEX_PRIVATE_FLAG);
  259. }
  260. /* Private futexes have been introduced earlier than the
  261. FUTEX_CLOCK_REALTIME flag. We don't have to run the test if we
  262. know the former are not supported. This also means we know the
  263. kernel will return ENOSYS for unknown operations. */
  264. if (THREAD_GETMEM (pd, header.private_futex) != 0)
  265. #endif
  266. #ifndef __ASSUME_FUTEX_CLOCK_REALTIME
  267. {
  268. int word = 0;
  269. /* NB: the syscall actually takes six parameters. The last is the
  270. bit mask. But since we will not actually wait at all the value
  271. is irrelevant. Given that passing six parameters is difficult
  272. on some architectures we just pass whatever random value the
  273. calling convention calls for to the kernel. It causes no harm. */
  274. word = INTERNAL_SYSCALL (futex, err, 5, &word,
  275. FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME
  276. | FUTEX_PRIVATE_FLAG, 1, NULL, 0);
  277. assert (INTERNAL_SYSCALL_ERROR_P (word, err));
  278. if (INTERNAL_SYSCALL_ERRNO (word, err) != ENOSYS)
  279. __set_futex_clock_realtime ();
  280. }
  281. #endif
  282. /* Set initial thread's stack block from 0 up to __libc_stack_end.
  283. It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
  284. purposes this is good enough. */
  285. THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
  286. /* Initialize the list of all running threads with the main thread. */
  287. INIT_LIST_HEAD (&__stack_user);
  288. list_add (&pd->list, &__stack_user);
  289. /* Before initializing __stack_user, the debugger could not find us and
  290. had to set __nptl_initial_report_events. Propagate its setting. */
  291. THREAD_SETMEM (pd, report_events, __nptl_initial_report_events);
  292. /* Install the cancellation signal handler. If for some reason we
  293. cannot install the handler we do not abort. Maybe we should, but
  294. it is only asynchronous cancellation which is affected. */
  295. struct sigaction sa;
  296. sa.sa_sigaction = sigcancel_handler;
  297. sa.sa_flags = SA_SIGINFO;
  298. __sigemptyset (&sa.sa_mask);
  299. (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
  300. /* Install the handle to change the threads' uid/gid. */
  301. sa.sa_sigaction = sighandler_setxid;
  302. sa.sa_flags = SA_SIGINFO | SA_RESTART;
  303. (void) __libc_sigaction (SIGSETXID, &sa, NULL);
  304. /* The parent process might have left the signals blocked. Just in
  305. case, unblock it. We reuse the signal mask in the sigaction
  306. structure. It is already cleared. */
  307. __sigaddset (&sa.sa_mask, SIGCANCEL);
  308. __sigaddset (&sa.sa_mask, SIGSETXID);
  309. (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
  310. NULL, _NSIG / 8);
  311. /* Get the size of the static and alignment requirements for the TLS
  312. block. */
  313. size_t static_tls_align;
  314. _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
  315. /* Make sure the size takes all the alignments into account. */
  316. if (STACK_ALIGN > static_tls_align)
  317. static_tls_align = STACK_ALIGN;
  318. __static_tls_align_m1 = static_tls_align - 1;
  319. __static_tls_size = roundup (__static_tls_size, static_tls_align);
  320. /* Determine the default allowed stack size. This is the size used
  321. in case the user does not specify one. */
  322. struct rlimit limit;
  323. if (getrlimit (RLIMIT_STACK, &limit) != 0
  324. || limit.rlim_cur == RLIM_INFINITY)
  325. /* The system limit is not usable. Use an architecture-specific
  326. default. */
  327. limit.rlim_cur = ARCH_STACK_DEFAULT_SIZE;
  328. else if (limit.rlim_cur < PTHREAD_STACK_MIN)
  329. /* The system limit is unusably small.
  330. Use the minimal size acceptable. */
  331. limit.rlim_cur = PTHREAD_STACK_MIN;
  332. /* Make sure it meets the minimum size that allocate_stack
  333. (allocatestack.c) will demand, which depends on the page size. */
  334. const uintptr_t pagesz = sysconf (_SC_PAGESIZE);
  335. const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
  336. if (limit.rlim_cur < minstack)
  337. limit.rlim_cur = minstack;
  338. /* Round the resource limit up to page size. */
  339. limit.rlim_cur = (limit.rlim_cur + pagesz - 1) & -pagesz;
  340. __default_stacksize = limit.rlim_cur;
  341. #ifdef SHARED
  342. /* Transfer the old value from the dynamic linker's internal location. */
  343. *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
  344. GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
  345. #endif
  346. GL(dl_init_static_tls) = &__pthread_init_static_tls;
  347. /* Register the fork generation counter with the libc. */
  348. #ifndef TLS_MULTIPLE_THREADS_IN_TCB
  349. __libc_multiple_threads_ptr =
  350. #endif
  351. __libc_pthread_init (&__fork_generation, __reclaim_stacks,
  352. ptr_pthread_functions);
  353. /* Determine whether the machine is SMP or not. */
  354. __is_smp = is_smp_system ();
  355. /* uClibc-specific stdio initialization for threads. */
  356. {
  357. FILE *fp;
  358. _stdio_user_locking = 0; /* 2 if threading not initialized */
  359. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
  360. if (fp->__user_locking != 1) {
  361. fp->__user_locking = 0;
  362. }
  363. }
  364. }
  365. }
  366. strong_alias (__pthread_initialize_minimal_internal,
  367. __pthread_initialize_minimal)