tls.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Definition for thread-local data handling. linuxthreads/i386 version.
  2. Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. #ifndef _TLS_H
  17. #define _TLS_H
  18. # include <pt-machine.h>
  19. #ifndef __ASSEMBLER__
  20. # include <stdbool.h>
  21. # include <stddef.h>
  22. # include <stdint.h>
  23. /* Type for the dtv. */
  24. typedef union dtv
  25. {
  26. size_t counter;
  27. struct
  28. {
  29. void *val;
  30. bool is_static;
  31. } pointer;
  32. } dtv_t;
  33. typedef struct
  34. {
  35. void *tcb; /* Pointer to the TCB. Not necessary the
  36. thread descriptor used by libpthread. */
  37. dtv_t *dtv;
  38. void *self; /* Pointer to the thread descriptor. */
  39. int multiple_threads;
  40. uintptr_t sysinfo;
  41. } tcbhead_t;
  42. #else /* __ASSEMBLER__ */
  43. # include <tcb-offsets.h>
  44. #endif
  45. /* We can support TLS only if the floating-stack support is available.
  46. However, we want to compile in the support and test at runtime whether
  47. the running kernel can support it or not. To avoid bothering with the
  48. TLS support code at all, use configure --without-tls.
  49. We need USE_TLS to be consistently defined, for ldsodefs.h conditionals.
  50. But some of the code below can cause problems in building libpthread
  51. (e.g. useldt.h will defined FLOATING_STACKS when it shouldn't). */
  52. #if defined HAVE_TLS_SUPPORT \
  53. && (defined FLOATING_STACKS || !defined IS_IN_libpthread)
  54. /* Signal that TLS support is available. */
  55. # define USE_TLS 1
  56. # ifndef __ASSEMBLER__
  57. /* Get system call information. */
  58. # include <sysdep.h>
  59. /* Get the thread descriptor definition. */
  60. # include <linuxthreads/descr.h>
  61. /* This is the size of the initial TCB. */
  62. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  63. /* Alignment requirements for the initial TCB. */
  64. # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
  65. /* This is the size of the TCB. */
  66. # define TLS_TCB_SIZE sizeof (struct _pthread_descr_struct)
  67. /* Alignment requirements for the TCB. */
  68. # define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
  69. /* The TCB can have any size and the memory following the address the
  70. thread pointer points to is unspecified. Allocate the TCB there. */
  71. # define TLS_TCB_AT_TP 1
  72. /* Install the dtv pointer. The pointer passed is to the element with
  73. index -1 which contain the length. */
  74. # define INSTALL_DTV(descr, dtvp) \
  75. ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
  76. /* Install new dtv for current thread. */
  77. # define INSTALL_NEW_DTV(dtv) \
  78. ({ struct _pthread_descr_struct *__descr; \
  79. THREAD_SETMEM (__descr, p_header.data.dtvp, (dtv)); })
  80. /* Return dtv of given thread descriptor. */
  81. # define GET_DTV(descr) \
  82. (((tcbhead_t *) (descr))->dtv)
  83. # ifdef __PIC__
  84. # define TLS_EBX_ARG "r"
  85. # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
  86. # else
  87. # define TLS_EBX_ARG "b"
  88. # define TLS_LOAD_EBX
  89. # endif
  90. # if !defined IS_IN_libpthread && !defined DO_MODIFY_LDT
  91. # include "useldt.h" /* For the structure. */
  92. # endif
  93. # if __ASSUME_LDT_WORKS > 0
  94. # define TLS_DO_MODIFY_LDT_KERNEL_CHECK(doit) (doit) /* Nothing to check. */
  95. # else
  96. # define TLS_DO_MODIFY_LDT_KERNEL_CHECK(doit) \
  97. (__builtin_expect (GLRO(dl_osversion) < 131939, 0) \
  98. ? "kernel too old for thread-local storage support\n" \
  99. : (doit))
  100. # endif
  101. # define TLS_DO_MODIFY_LDT(descr, nr) \
  102. TLS_DO_MODIFY_LDT_KERNEL_CHECK( \
  103. ({ \
  104. struct modify_ldt_ldt_s ldt_entry = \
  105. { nr, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
  106. 1, 0, 0, 1, 0, 1, 0 }; \
  107. int result; \
  108. __asm__ __volatile__ (TLS_LOAD_EBX \
  109. "int $0x80\n\t" \
  110. TLS_LOAD_EBX \
  111. : "=a" (result) \
  112. : "0" (__NR_modify_ldt), \
  113. /* The extra argument with the "m" constraint is necessary \
  114. to let the compiler know that we are accessing LDT_ENTRY \
  115. here. */ \
  116. "m" (ldt_entry), TLS_EBX_ARG (1), "c" (&ldt_entry), \
  117. "d" (sizeof (ldt_entry))); \
  118. __builtin_expect (result, 0) == 0 \
  119. ? ({ __asm__ ("movw %w0, %%gs" : : "q" ((nr) * 8 + 7)); NULL; }) \
  120. : "cannot set up LDT for thread-local storage\n"; \
  121. }))
  122. # define TLS_DO_SET_THREAD_AREA(descr, secondcall) \
  123. ({ \
  124. struct modify_ldt_ldt_s ldt_entry = \
  125. { -1, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
  126. 1, 0, 0, 1, 0, 1, 0 }; \
  127. int result; \
  128. if (secondcall) \
  129. ldt_entry.entry_number = ({ int _gs; \
  130. __asm__ ("movw %%gs, %w0" : "=q" (_gs)); \
  131. (_gs & 0xffff) >> 3; }); \
  132. __asm__ __volatile__ (TLS_LOAD_EBX \
  133. "int $0x80\n\t" \
  134. TLS_LOAD_EBX \
  135. : "=a" (result), "=m" (ldt_entry.entry_number) \
  136. : "0" (__NR_set_thread_area), \
  137. /* The extra argument with the "m" constraint is necessary \
  138. to let the compiler know that we are accessing LDT_ENTRY \
  139. here. */ \
  140. TLS_EBX_ARG (&ldt_entry), "m" (ldt_entry)); \
  141. if (__builtin_expect (result, 0) == 0) \
  142. __asm__ ("movw %w0, %%gs" : : "q" (ldt_entry.entry_number * 8 + 3)); \
  143. result; \
  144. })
  145. # ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
  146. # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
  147. (TLS_DO_SET_THREAD_AREA (descr, secondcall) \
  148. ? "set_thread_area failed when setting up thread-local storage\n" : NULL)
  149. # elif defined __NR_set_thread_area
  150. # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
  151. (TLS_DO_SET_THREAD_AREA (descr, secondcall) \
  152. ? TLS_DO_MODIFY_LDT (descr, 0) : NULL)
  153. # else
  154. # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
  155. TLS_DO_MODIFY_LDT ((descr), 0)
  156. # endif
  157. #if defined NEED_DL_SYSINFO
  158. # define INIT_SYSINFO \
  159. head->sysinfo = GLRO(dl_sysinfo)
  160. #else
  161. # define INIT_SYSINFO
  162. #endif
  163. /* Code to initially initialize the thread pointer. This might need
  164. special attention since 'errno' is not yet available and if the
  165. operation can cause a failure 'errno' must not be touched.
  166. The value of this macro is null if successful, or an error string. */
  167. # define TLS_INIT_TP(descr, secondcall) \
  168. ({ \
  169. void *_descr = (descr); \
  170. tcbhead_t *head = _descr; \
  171. \
  172. head->tcb = _descr; \
  173. /* For now the thread descriptor is at the same address. */ \
  174. head->self = _descr; \
  175. \
  176. INIT_SYSINFO; \
  177. TLS_SETUP_GS_SEGMENT (_descr, secondcall); \
  178. })
  179. /* Indicate that dynamic linker shouldn't try to initialize TLS even
  180. when no PT_TLS segments are found in the program and libraries
  181. it is linked against. */
  182. # define TLS_INIT_TP_EXPENSIVE 1
  183. /* Return the address of the dtv for the current thread. */
  184. # define THREAD_DTV() \
  185. ({ struct _pthread_descr_struct *__descr; \
  186. THREAD_GETMEM (__descr, p_header.data.dtvp); })
  187. # endif /* HAVE_TLS_SUPPORT && (FLOATING_STACKS || !IS_IN_libpthread) */
  188. #endif /* __ASSEMBLER__ */
  189. #endif /* tls.h */