init.c 13 KB

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