init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 <smp.h>
  29. #include <lowlevellock.h>
  30. #include <version.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. int
  59. __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact);
  60. #ifdef SHARED
  61. static const struct pthread_functions pthread_functions =
  62. {
  63. .ptr_pthread_attr_destroy = __pthread_attr_destroy,
  64. .ptr___pthread_attr_init_2_1 = __pthread_attr_init_2_1,
  65. .ptr_pthread_attr_getdetachstate = __pthread_attr_getdetachstate,
  66. .ptr_pthread_attr_setdetachstate = __pthread_attr_setdetachstate,
  67. .ptr_pthread_attr_getinheritsched = __pthread_attr_getinheritsched,
  68. .ptr_pthread_attr_setinheritsched = __pthread_attr_setinheritsched,
  69. .ptr_pthread_attr_getschedparam = __pthread_attr_getschedparam,
  70. .ptr_pthread_attr_setschedparam = __pthread_attr_setschedparam,
  71. .ptr_pthread_attr_getschedpolicy = __pthread_attr_getschedpolicy,
  72. .ptr_pthread_attr_setschedpolicy = __pthread_attr_setschedpolicy,
  73. .ptr_pthread_attr_getscope = __pthread_attr_getscope,
  74. .ptr_pthread_attr_setscope = __pthread_attr_setscope,
  75. .ptr_pthread_condattr_destroy = __pthread_condattr_destroy,
  76. .ptr_pthread_condattr_init = __pthread_condattr_init,
  77. .ptr___pthread_cond_broadcast = __pthread_cond_broadcast,
  78. .ptr___pthread_cond_destroy = __pthread_cond_destroy,
  79. .ptr___pthread_cond_init = __pthread_cond_init,
  80. .ptr___pthread_cond_signal = __pthread_cond_signal,
  81. .ptr___pthread_cond_wait = __pthread_cond_wait,
  82. .ptr___pthread_cond_timedwait = __pthread_cond_timedwait,
  83. .ptr_pthread_equal = __pthread_equal,
  84. .ptr___pthread_exit = __pthread_exit,
  85. .ptr_pthread_getschedparam = __pthread_getschedparam,
  86. .ptr_pthread_setschedparam = __pthread_setschedparam,
  87. .ptr_pthread_mutex_destroy = __pthread_mutex_destroy,
  88. .ptr_pthread_mutex_init = __pthread_mutex_init,
  89. .ptr_pthread_mutex_lock = __pthread_mutex_lock,
  90. .ptr_pthread_mutex_unlock = __pthread_mutex_unlock,
  91. .ptr_pthread_self = __pthread_self,
  92. .ptr_pthread_setcancelstate = __pthread_setcancelstate,
  93. .ptr_pthread_setcanceltype = __pthread_setcanceltype,
  94. .ptr___pthread_cleanup_upto = __pthread_cleanup_upto,
  95. .ptr___pthread_once = __pthread_once_internal,
  96. .ptr___pthread_rwlock_rdlock = __pthread_rwlock_rdlock_internal,
  97. .ptr___pthread_rwlock_wrlock = __pthread_rwlock_wrlock_internal,
  98. .ptr___pthread_rwlock_unlock = __pthread_rwlock_unlock_internal,
  99. .ptr___pthread_key_create = __pthread_key_create_internal,
  100. .ptr___pthread_getspecific = __pthread_getspecific_internal,
  101. .ptr___pthread_setspecific = __pthread_setspecific_internal,
  102. .ptr__pthread_cleanup_push_defer = __pthread_cleanup_push_defer,
  103. .ptr__pthread_cleanup_pop_restore = __pthread_cleanup_pop_restore,
  104. .ptr_nthreads = &__nptl_nthreads,
  105. .ptr___pthread_unwind = &__pthread_unwind,
  106. .ptr__nptl_deallocate_tsd = __nptl_deallocate_tsd,
  107. .ptr__nptl_setxid = __nptl_setxid
  108. };
  109. # define ptr_pthread_functions &pthread_functions
  110. #else
  111. # define ptr_pthread_functions NULL
  112. #endif
  113. /* For asynchronous cancellation we use a signal. This is the handler. */
  114. static void
  115. sigcancel_handler (int sig, siginfo_t *si, void *ctx)
  116. {
  117. /* Safety check. It would be possible to call this function for
  118. other signals and send a signal from another process. This is not
  119. correct and might even be a security problem. Try to catch as
  120. many incorrect invocations as possible. */
  121. if (sig != SIGCANCEL
  122. #ifdef __ASSUME_CORRECT_SI_PID
  123. /* Kernels before 2.5.75 stored the thread ID and not the process
  124. ID in si_pid so we skip this test. */
  125. || si->si_pid != THREAD_GETMEM (THREAD_SELF, pid)
  126. #endif
  127. || si->si_code != SI_TKILL)
  128. return;
  129. struct pthread *self = THREAD_SELF;
  130. int oldval = THREAD_GETMEM (self, cancelhandling);
  131. while (1)
  132. {
  133. /* We are canceled now. When canceled by another thread this flag
  134. is already set but if the signal is directly send (internally or
  135. from another process) is has to be done here. */
  136. int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
  137. if (oldval == newval || (oldval & EXITING_BITMASK) != 0)
  138. /* Already canceled or exiting. */
  139. break;
  140. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
  141. oldval);
  142. if (curval == oldval)
  143. {
  144. /* Set the return value. */
  145. THREAD_SETMEM (self, result, PTHREAD_CANCELED);
  146. /* Make sure asynchronous cancellation is still enabled. */
  147. if ((newval & CANCELTYPE_BITMASK) != 0)
  148. /* Run the registered destructors and terminate the thread. */
  149. __do_cancel ();
  150. break;
  151. }
  152. oldval = curval;
  153. }
  154. }
  155. struct xid_command *__xidcmd attribute_hidden;
  156. /* For asynchronous cancellation we use a signal. This is the handler. */
  157. static void
  158. sighandler_setxid (int sig, siginfo_t *si, void *ctx)
  159. {
  160. /* Safety check. It would be possible to call this function for
  161. other signals and send a signal from another process. This is not
  162. correct and might even be a security problem. Try to catch as
  163. many incorrect invocations as possible. */
  164. if (sig != SIGSETXID
  165. #ifdef __ASSUME_CORRECT_SI_PID
  166. /* Kernels before 2.5.75 stored the thread ID and not the process
  167. ID in si_pid so we skip this test. */
  168. || si->si_pid != THREAD_GETMEM (THREAD_SELF, pid)
  169. #endif
  170. || si->si_code != SI_TKILL)
  171. return;
  172. INTERNAL_SYSCALL_DECL (err);
  173. INTERNAL_SYSCALL_NCS (__xidcmd->syscall_no, err, 3, __xidcmd->id[0],
  174. __xidcmd->id[1], __xidcmd->id[2]);
  175. if (atomic_decrement_val (&__xidcmd->cntr) == 0)
  176. lll_futex_wake (&__xidcmd->cntr, 1);
  177. }
  178. /* When using __thread for this, we do it in libc so as not
  179. to give libpthread its own TLS segment just for this. */
  180. extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
  181. void
  182. __pthread_initialize_minimal_internal (void)
  183. {
  184. #ifndef SHARED
  185. /* Unlike in the dynamically linked case the dynamic linker has not
  186. taken care of initializing the TLS data structures. */
  187. __libc_setup_tls (TLS_TCB_SIZE, TLS_TCB_ALIGN);
  188. /* We must prevent gcc from being clever and move any of the
  189. following code ahead of the __libc_setup_tls call. This function
  190. will initialize the thread register which is subsequently
  191. used. */
  192. __asm __volatile ("");
  193. #endif
  194. /* Minimal initialization of the thread descriptor. */
  195. struct pthread *pd = THREAD_SELF;
  196. INTERNAL_SYSCALL_DECL (err);
  197. pd->pid = pd->tid = INTERNAL_SYSCALL (set_tid_address, err, 1, &pd->tid);
  198. THREAD_SETMEM (pd, specific[0], &pd->specific_1stblock[0]);
  199. THREAD_SETMEM (pd, user_stack, true);
  200. if (LLL_LOCK_INITIALIZER != 0)
  201. THREAD_SETMEM (pd, lock, LLL_LOCK_INITIALIZER);
  202. #if HP_TIMING_AVAIL
  203. THREAD_SETMEM (pd, cpuclock_offset, GL(dl_cpuclock_offset));
  204. #endif
  205. /* Set initial thread's stack block from 0 up to __libc_stack_end.
  206. It will be bigger than it actually is, but for unwind.c/pt-longjmp.c
  207. purposes this is good enough. */
  208. THREAD_SETMEM (pd, stackblock_size, (size_t) __libc_stack_end);
  209. /* Initialize the list of all running threads with the main thread. */
  210. INIT_LIST_HEAD (&__stack_user);
  211. list_add (&pd->list, &__stack_user);
  212. /* Install the cancellation signal handler. If for some reason we
  213. cannot install the handler we do not abort. Maybe we should, but
  214. it is only asynchronous cancellation which is affected. */
  215. struct sigaction sa;
  216. sa.sa_sigaction = sigcancel_handler;
  217. sa.sa_flags = SA_SIGINFO;
  218. __sigemptyset (&sa.sa_mask);
  219. (void) __libc_sigaction (SIGCANCEL, &sa, NULL);
  220. /* Install the handle to change the threads' uid/gid. */
  221. sa.sa_sigaction = sighandler_setxid;
  222. sa.sa_flags = SA_SIGINFO | SA_RESTART;
  223. (void) __libc_sigaction (SIGSETXID, &sa, NULL);
  224. /* The parent process might have left the signals blocked. Just in
  225. case, unblock it. We reuse the signal mask in the sigaction
  226. structure. It is already cleared. */
  227. __sigaddset (&sa.sa_mask, SIGCANCEL);
  228. __sigaddset (&sa.sa_mask, SIGSETXID);
  229. (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &sa.sa_mask,
  230. NULL, _NSIG / 8);
  231. /* Get the size of the static and alignment requirements for the TLS
  232. block. */
  233. size_t static_tls_align;
  234. _dl_get_tls_static_info (&__static_tls_size, &static_tls_align);
  235. /* Make sure the size takes all the alignments into account. */
  236. if (STACK_ALIGN > static_tls_align)
  237. static_tls_align = STACK_ALIGN;
  238. __static_tls_align_m1 = static_tls_align - 1;
  239. __static_tls_size = roundup (__static_tls_size, static_tls_align);
  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. limit.rlim_cur = 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. limit.rlim_cur = PTHREAD_STACK_MIN;
  252. /* Make sure it meets the minimum size that allocate_stack
  253. (allocatestack.c) will demand, which depends on the page size. */
  254. const uintptr_t pagesz = sysconf (_SC_PAGESIZE);
  255. const size_t minstack = pagesz + __static_tls_size + MINIMAL_REST_STACK;
  256. if (limit.rlim_cur < minstack)
  257. limit.rlim_cur = minstack;
  258. /* Round the resource limit up to page size. */
  259. limit.rlim_cur = (limit.rlim_cur + pagesz - 1) & -pagesz;
  260. __default_stacksize = limit.rlim_cur;
  261. #ifdef SHARED
  262. /* Transfer the old value from the dynamic linker's internal location. */
  263. *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd)) ();
  264. GL(dl_error_catch_tsd) = &__libc_dl_error_tsd;
  265. #endif
  266. GL(dl_init_static_tls) = &__pthread_init_static_tls;
  267. /* Register the fork generation counter with the libc. */
  268. #ifndef TLS_MULTIPLE_THREADS_IN_TCB
  269. __libc_multiple_threads_ptr =
  270. #endif
  271. __libc_pthread_init (&__fork_generation, __reclaim_stacks,
  272. ptr_pthread_functions);
  273. /* Determine whether the machine is SMP or not. */
  274. __is_smp = is_smp_system ();
  275. }
  276. strong_alias (__pthread_initialize_minimal_internal,
  277. __pthread_initialize_minimal)