init.c 15 KB

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