init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* Copyright (C) 2002, 2003, 2004 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 <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 <shlib-compat.h>
  30. #include <smp.h>
  31. #include <lowlevellock.h>
  32. #ifndef __NR_set_tid_address
  33. /* XXX For the time being... Once we can rely on the kernel headers
  34. having the definition remove these lines. */
  35. #if defined __s390__
  36. # define __NR_set_tid_address 252
  37. #elif defined __ia64__
  38. # define __NR_set_tid_address 1233
  39. #elif defined __i386__
  40. # define __NR_set_tid_address 258
  41. #elif defined __x86_64__
  42. # define __NR_set_tid_address 218
  43. #elif defined __powerpc__
  44. # define __NR_set_tid_address 232
  45. #elif defined __sparc__
  46. # define __NR_set_tid_address 166
  47. #else
  48. # error "define __NR_set_tid_address"
  49. #endif
  50. #endif
  51. /* Size and alignment of static TLS block. */
  52. size_t __static_tls_size;
  53. size_t __static_tls_align_m1;
  54. /* Version of the library, used in libthread_db to detect mismatches. */
  55. static const char nptl_version[] __attribute_used__ = VERSION;
  56. #if defined USE_TLS && !defined SHARED
  57. extern void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
  58. #endif
  59. #ifdef SHARED
  60. static const struct pthread_functions pthread_functions =
  61. {
  62. .ptr_pthread_attr_destroy = __pthread_attr_destroy,
  63. # if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
  64. .ptr___pthread_attr_init_2_0 = __pthread_attr_init_2_0,
  65. # endif
  66. .ptr___pthread_attr_init_2_1 = __pthread_attr_init_2_1,
  67. .ptr_pthread_attr_getdetachstate = __pthread_attr_getdetachstate,
  68. .ptr_pthread_attr_setdetachstate = __pthread_attr_setdetachstate,
  69. .ptr_pthread_attr_getinheritsched = __pthread_attr_getinheritsched,
  70. .ptr_pthread_attr_setinheritsched = __pthread_attr_setinheritsched,
  71. .ptr_pthread_attr_getschedparam = __pthread_attr_getschedparam,
  72. .ptr_pthread_attr_setschedparam = __pthread_attr_setschedparam,
  73. .ptr_pthread_attr_getschedpolicy = __pthread_attr_getschedpolicy,
  74. .ptr_pthread_attr_setschedpolicy = __pthread_attr_setschedpolicy,
  75. .ptr_pthread_attr_getscope = __pthread_attr_getscope,
  76. .ptr_pthread_attr_setscope = __pthread_attr_setscope,
  77. .ptr_pthread_condattr_destroy = __pthread_condattr_destroy,
  78. .ptr_pthread_condattr_init = __pthread_condattr_init,
  79. .ptr___pthread_cond_broadcast = __pthread_cond_broadcast,
  80. .ptr___pthread_cond_destroy = __pthread_cond_destroy,
  81. .ptr___pthread_cond_init = __pthread_cond_init,
  82. .ptr___pthread_cond_signal = __pthread_cond_signal,
  83. .ptr___pthread_cond_wait = __pthread_cond_wait,
  84. .ptr___pthread_cond_timedwait = __pthread_cond_timedwait,
  85. # if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
  86. .ptr___pthread_cond_broadcast_2_0 = __pthread_cond_broadcast_2_0,
  87. .ptr___pthread_cond_destroy_2_0 = __pthread_cond_destroy_2_0,
  88. .ptr___pthread_cond_init_2_0 = __pthread_cond_init_2_0,
  89. .ptr___pthread_cond_signal_2_0 = __pthread_cond_signal_2_0,
  90. .ptr___pthread_cond_wait_2_0 = __pthread_cond_wait_2_0,
  91. .ptr___pthread_cond_timedwait_2_0 = __pthread_cond_timedwait_2_0,
  92. # endif
  93. .ptr_pthread_equal = __pthread_equal,
  94. .ptr___pthread_exit = __pthread_exit,
  95. .ptr_pthread_getschedparam = __pthread_getschedparam,
  96. .ptr_pthread_setschedparam = __pthread_setschedparam,
  97. .ptr_pthread_mutex_destroy = INTUSE(__pthread_mutex_destroy),
  98. .ptr_pthread_mutex_init = INTUSE(__pthread_mutex_init),
  99. .ptr_pthread_mutex_lock = INTUSE(__pthread_mutex_lock),
  100. .ptr_pthread_mutex_unlock = INTUSE(__pthread_mutex_unlock),
  101. .ptr_pthread_self = __pthread_self,
  102. .ptr_pthread_setcancelstate = __pthread_setcancelstate,
  103. .ptr_pthread_setcanceltype = __pthread_setcanceltype,
  104. .ptr___pthread_cleanup_upto = __pthread_cleanup_upto,
  105. .ptr___pthread_once = __pthread_once_internal,
  106. .ptr___pthread_rwlock_rdlock = __pthread_rwlock_rdlock_internal,
  107. .ptr___pthread_rwlock_wrlock = __pthread_rwlock_wrlock_internal,
  108. .ptr___pthread_rwlock_unlock = __pthread_rwlock_unlock_internal,
  109. .ptr___pthread_key_create = __pthread_key_create_internal,
  110. .ptr___pthread_getspecific = __pthread_getspecific_internal,
  111. .ptr___pthread_setspecific = __pthread_setspecific_internal,
  112. .ptr__pthread_cleanup_push_defer = __pthread_cleanup_push_defer,
  113. .ptr__pthread_cleanup_pop_restore = __pthread_cleanup_pop_restore,
  114. .ptr_nthreads = &__nptl_nthreads,
  115. .ptr___pthread_unwind = &__pthread_unwind,
  116. .ptr__nptl_deallocate_tsd = __nptl_deallocate_tsd,
  117. .ptr__nptl_setxid = __nptl_setxid
  118. };
  119. # define ptr_pthread_functions &pthread_functions
  120. #else
  121. # define ptr_pthread_functions NULL
  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. /* Safety check. It would be possible to call this function for
  128. other signals and send a signal from another process. This is not
  129. correct and might even be a security problem. Try to catch as
  130. many incorrect invocations as possible. */
  131. if (sig != SIGCANCEL
  132. #ifdef __ASSUME_CORRECT_SI_PID
  133. /* Kernels before 2.5.75 stored the thread ID and not the process
  134. ID in si_pid so we skip this test. */
  135. || si->si_pid != THREAD_GETMEM (THREAD_SELF, pid)
  136. #endif
  137. || si->si_code != SI_TKILL)
  138. return;
  139. struct pthread *self = THREAD_SELF;
  140. int oldval = THREAD_GETMEM (self, cancelhandling);
  141. while (1)
  142. {
  143. /* We are canceled now. When canceled by another thread this flag
  144. is already set but if the signal is directly send (internally or
  145. from another process) is has to be done here. */
  146. int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
  147. if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
  148. /* Already canceled or exiting. */
  149. break;
  150. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
  151. oldval);
  152. if (curval == oldval)
  153. {
  154. /* Set the return value. */
  155. THREAD_SETMEM (self, result, PTHREAD_CANCELED);
  156. /* Make sure asynchronous cancellation is still enabled. */
  157. if ((newval & CANCELTYPE_BITMASK) != 0)
  158. /* Run the registered destructors and terminate the thread. */
  159. __do_cancel ();
  160. break;
  161. }
  162. oldval = curval;
  163. }
  164. }
  165. struct xid_command *__xidcmd attribute_hidden;
  166. /* For asynchronous cancellation we use a signal. This is the handler. */
  167. static void
  168. sighandler_setxid (int sig, siginfo_t *si, void *ctx)
  169. {
  170. /* Safety check. It would be possible to call this function for
  171. other signals and send a signal from another process. This is not
  172. correct and might even be a security problem. Try to catch as
  173. many incorrect invocations as possible. */
  174. if (sig != SIGSETXID
  175. #ifdef __ASSUME_CORRECT_SI_PID
  176. /* Kernels before 2.5.75 stored the thread ID and not the process
  177. ID in si_pid so we skip this test. */
  178. || si->si_pid != THREAD_GETMEM (THREAD_SELF, pid)
  179. #endif
  180. || si->si_code != SI_TKILL)
  181. return;
  182. INTERNAL_SYSCALL_DECL (err);
  183. INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, err, 3, __xidcmd->id[0],
  184. __xidcmd->id[1], __xidcmd->id[2]);
  185. if (atomic_decrement_val (&__xidcmd->cntr) == 0)
  186. lll_futex_wake (&__xidcmd->cntr, 1);
  187. }
  188. /* When using __thread for this, we do it in libc so as not
  189. to give libpthread its own TLS segment just for this. */
  190. extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
  191. void
  192. __pthread_initialize_minimal_internal (void)
  193. {
  194. #ifndef SHARED
  195. /* Unlike in the dynamically linked case the dynamic linker has not
  196. taken care of initializing the TLS data structures. */
  197. __libc_setup_tls (TLS_TCB_SIZE, TLS_TCB_ALIGN);
  198. /* We must prevent gcc from being clever and move any of the
  199. following code ahead of the __libc_setup_tls call. This function
  200. will initialize the thread register which is subsequently
  201. used. */
  202. __asm __volatile ("");
  203. #endif
  204. /* Minimal initialization of the thread descriptor. */
  205. struct pthread *pd = THREAD_SELF;
  206. INTERNAL_SYSCALL_DECL (err);
  207. pd->pid = pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid);
  208. THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
  209. THREAD_SETMEM (pd, user_stack, true);
  210. if (LLL_LOCK_INITIALIZER != 0)
  211. THREAD_SETMEM (pd, lock, LLL_LOCK_INITIALIZER);
  212. #if HP_TIMING_AVAIL
  213. THREAD_SETMEM (pd, cpuclock_offset, GL(dl_cpuclock_offset));
  214. #endif
  215. /* Set initial thread's stack block from 0 up to __libc_stack_end.
  216. It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
  217. purposes this is good enough. */
  218. THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
  219. /* Initialize the list of all running threads with the main thread. */
  220. INIT_LIST_HEAD (&__stack_user);
  221. list_add (&pd->list, &__stack_user);
  222. /* Install the cancellation signal handler. If for some reason we
  223. cannot install the handler we do not abort. Maybe we should, but
  224. it is only asynchronous cancellation which is affected. */
  225. struct sigaction sa;
  226. sa.sa_sigaction = sigcancel_handler;
  227. sa.sa_flags = SA_SIGINFO;
  228. __sigemptyset (&sa.sa_mask);
  229. (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
  230. /* Install the handle to change the threads' uid/gid. */
  231. sa.sa_sigaction = sighandler_setxid;
  232. sa.sa_flags = SA_SIGINFO | SA_RESTART;
  233. (void) __libc_sigaction (SIGSETXID, &sa, NULL);
  234. /* The parent process might have left the signals blocked. Just in
  235. case, unblock it. We reuse the signal mask in the sigaction
  236. structure. It is already cleared. */
  237. __sigaddset (&sa.sa_mask, SIGCANCEL);
  238. __sigaddset (&sa.sa_mask, SIGSETXID);
  239. (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
  240. NULL, _NSIG / 8);
  241. /* Determine the default allowed stack size. This is the size used
  242. in case the user does not specify one. */
  243. struct rlimit limit;
  244. if (getrlimit (RLIMIT_STACK, &limit) != 0
  245. || limit.rlim_cur == RLIM_INFINITY)
  246. /* The system limit is not usable. Use an architecture-specific
  247. default. */
  248. __default_stacksize = ARCH_STACK_DEFAULT_SIZE;
  249. else if (limit.rlim_cur < PTHREAD_STACK_MIN)
  250. /* The system limit is unusably small.
  251. Use the minimal size acceptable. */
  252. __default_stacksize = PTHREAD_STACK_MIN;
  253. else
  254. {
  255. /* Round the resource limit up to page size. */
  256. const uintptr_t pagesz = __sysconf (_SC_PAGESIZE);
  257. __default_stacksize = (limit.rlim_cur + pagesz - 1) & -pagesz;
  258. }
  259. /* Get the size of the static and alignment requirements for the TLS
  260. block. */
  261. size_t static_tls_align;
  262. _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
  263. /* Make sure the size takes all the alignments into account. */
  264. if (STACK_ALIGN > static_tls_align)
  265. static_tls_align = STACK_ALIGN;
  266. __static_tls_align_m1 = static_tls_align - 1;
  267. __static_tls_size = roundup (__static_tls_size, static_tls_align);
  268. #ifdef SHARED
  269. /* Transfer the old value from the dynamic linker's internal location. */
  270. *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
  271. GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
  272. /* Make __rtld_lock_{,un}lock_recursive use pthread_mutex_{,un}lock,
  273. keep the lock count from the ld.so implementation. */
  274. GL(dl_rtld_lock_recursive) = (void *) INTUSE (__pthread_mutex_lock);
  275. GL(dl_rtld_unlock_recursive) = (void *) INTUSE (__pthread_mutex_unlock);
  276. unsigned int rtld_lock_count = GL(dl_load_lock).mutex.__data.__count;
  277. GL(dl_load_lock).mutex.__data.__count = 0;
  278. while (rtld_lock_count-- > 0)
  279. INTUSE (__pthread_mutex_lock) (&GL(dl_load_lock).mutex);
  280. GL(dl_make_stack_executable_hook) = &__make_stacks_executable;
  281. #endif
  282. GL(dl_init_static_tls) = &__pthread_init_static_tls;
  283. /* Register the fork generation counter with the libc. */
  284. #ifndef TLS_MULTIPLE_THREADS_IN_TCB
  285. __libc_multiple_threads_ptr =
  286. #endif
  287. __libc_pthread_init (&__fork_generation, __reclaim_stacks,
  288. ptr_pthread_functions);
  289. /* Determine whether the machine is SMP or not. */
  290. __is_smp = is_smp_system ();
  291. }
  292. strong_alias (__pthread_initialize_minimal_internal,
  293. __pthread_initialize_minimal)