tls.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* Definition for thread-local data handling. nptl/x86_64 version.
  2. Copyright (C) 2002, 2003, 2004 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 1
  18. #include <asm/prctl.h> /* For ARCH_SET_FS. */
  19. #ifndef __ASSEMBLER__
  20. # include <stdbool.h>
  21. # include <stddef.h>
  22. # include <stdint.h>
  23. # include <stdlib.h>
  24. /* Type for the dtv. */
  25. typedef union dtv
  26. {
  27. size_t counter;
  28. struct
  29. {
  30. void *val;
  31. bool is_static;
  32. } pointer;
  33. } dtv_t;
  34. typedef struct
  35. {
  36. void *tcb; /* Pointer to the TCB. Not necessary the
  37. thread descriptor used by libpthread. */
  38. dtv_t *dtv;
  39. void *self; /* Pointer to the thread descriptor. */
  40. int multiple_threads;
  41. } tcbhead_t;
  42. #else /* __ASSEMBLER__ */
  43. # include <tcb-offsets.h>
  44. #endif
  45. /* We require TLS support in the tools. */
  46. #ifndef HAVE_TLS_SUPPORT
  47. # error "TLS support is required."
  48. #endif
  49. /* Signal that TLS support is available. */
  50. #define USE_TLS 1
  51. /* Alignment requirement for the stack. */
  52. #define STACK_ALIGN 16
  53. #ifndef __ASSEMBLER__
  54. /* Get system call information. */
  55. # include <sysdep.h>
  56. /* Get the thread descriptor definition. */
  57. # include <nptl/descr.h>
  58. #ifndef LOCK_PREFIX
  59. # ifdef UP
  60. # define LOCK_PREFIX /* nothing */
  61. # else
  62. # define LOCK_PREFIX "lock;"
  63. # endif
  64. #endif
  65. /* This is the size of the initial TCB. */
  66. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  67. /* Alignment requirements for the initial TCB. */
  68. # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
  69. /* This is the size of the TCB. */
  70. # define TLS_TCB_SIZE sizeof (struct pthread)
  71. /* Alignment requirements for the TCB. */
  72. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  73. /* The TCB can have any size and the memory following the address the
  74. thread pointer points to is unspecified. Allocate the TCB there. */
  75. # define TLS_TCB_AT_TP 1
  76. /* Install the dtv pointer. The pointer passed is to the element with
  77. index -1 which contain the length. */
  78. # define INSTALL_DTV(descr, dtvp) \
  79. ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
  80. /* Install new dtv for current thread. */
  81. # define INSTALL_NEW_DTV(dtvp) \
  82. ({ struct pthread *__pd; \
  83. THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
  84. /* Return dtv of given thread descriptor. */
  85. # define GET_DTV(descr) \
  86. (((tcbhead_t *) (descr))->dtv)
  87. /* Macros to load from and store into segment registers. */
  88. # define TLS_GET_FS() \
  89. ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
  90. # define TLS_SET_FS(val) \
  91. __asm ("movl %0, %%fs" :: "q" (val))
  92. /* Code to initially initialize the thread pointer. This might need
  93. special attention since 'errno' is not yet available and if the
  94. operation can cause a failure 'errno' must not be touched.
  95. We have to make the syscall for both uses of the macro since the
  96. address might be (and probably is) different. */
  97. # define TLS_INIT_TP(thrdescr, secondcall) \
  98. ({ void *_thrdescr = (thrdescr); \
  99. tcbhead_t *_head = _thrdescr; \
  100. int _result; \
  101. \
  102. _head->tcb = _thrdescr; \
  103. /* For now the thread descriptor is at the same address. */ \
  104. _head->self = _thrdescr; \
  105. \
  106. /* It is a simple syscall to set the %fs value for the thread. */ \
  107. asm volatile ("syscall" \
  108. : "=a" (_result) \
  109. : "0" ((unsigned long int) __NR_arch_prctl), \
  110. "D" ((unsigned long int) ARCH_SET_FS), \
  111. "S" (_thrdescr) \
  112. : "memory", "cc", "r11", "cx"); \
  113. \
  114. _result ? "cannot set %fs base address for thread-local storage" : 0; \
  115. })
  116. /* Return the address of the dtv for the current thread. */
  117. # define THREAD_DTV() \
  118. ({ struct pthread *__pd; \
  119. THREAD_GETMEM (__pd, header.dtv); })
  120. /* Return the thread descriptor for the current thread.
  121. The contained asm must *not* be marked volatile since otherwise
  122. assignments like
  123. pthread_descr self = thread_self();
  124. do not get optimized away. */
  125. # define THREAD_SELF \
  126. ({ struct pthread *__self; \
  127. asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
  128. : "i" (offsetof (struct pthread, header.self))); \
  129. __self;})
  130. /* Magic for libthread_db to know how to do THREAD_SELF. */
  131. # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
  132. # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
  133. /* Read member of the thread descriptor directly. */
  134. # define THREAD_GETMEM(descr, member) \
  135. ({ __typeof (descr->member) __value; \
  136. if (sizeof (__value) == 1) \
  137. asm volatile ("movb %%fs:%P2,%b0" \
  138. : "=q" (__value) \
  139. : "0" (0), "i" (offsetof (struct pthread, member))); \
  140. else if (sizeof (__value) == 4) \
  141. asm volatile ("movl %%fs:%P1,%0" \
  142. : "=r" (__value) \
  143. : "i" (offsetof (struct pthread, member))); \
  144. else \
  145. { \
  146. if (sizeof (__value) != 8) \
  147. /* There should not be any value with a size other than 1, \
  148. 4 or 8. */ \
  149. abort (); \
  150. \
  151. asm volatile ("movq %%fs:%P1,%q0" \
  152. : "=r" (__value) \
  153. : "i" (offsetof (struct pthread, member))); \
  154. } \
  155. __value; })
  156. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  157. # define THREAD_GETMEM_NC(descr, member, idx) \
  158. ({ __typeof (descr->member[0]) __value; \
  159. if (sizeof (__value) == 1) \
  160. asm volatile ("movb %%fs:%P2(%q3),%b0" \
  161. : "=q" (__value) \
  162. : "0" (0), "i" (offsetof (struct pthread, member[0])), \
  163. "r" (idx)); \
  164. else if (sizeof (__value) == 4) \
  165. asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
  166. : "=r" (__value) \
  167. : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
  168. else \
  169. { \
  170. if (sizeof (__value) != 8) \
  171. /* There should not be any value with a size other than 1, \
  172. 4 or 8. */ \
  173. abort (); \
  174. \
  175. asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
  176. : "=r" (__value) \
  177. : "i" (offsetof (struct pthread, member[0])), \
  178. "r" (idx)); \
  179. } \
  180. __value; })
  181. /* Loading addresses of objects on x86-64 needs to be treated special
  182. when generating PIC code. */
  183. #ifdef __pic__
  184. # define IMM_MODE "nr"
  185. #else
  186. # define IMM_MODE "ir"
  187. #endif
  188. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  189. # define THREAD_SETMEM(descr, member, value) \
  190. ({ if (sizeof (descr->member) == 1) \
  191. asm volatile ("movb %b0,%%fs:%P1" : \
  192. : "iq" (value), \
  193. "i" (offsetof (struct pthread, member))); \
  194. else if (sizeof (descr->member) == 4) \
  195. asm volatile ("movl %0,%%fs:%P1" : \
  196. : IMM_MODE (value), \
  197. "i" (offsetof (struct pthread, member))); \
  198. else \
  199. { \
  200. if (sizeof (descr->member) != 8) \
  201. /* There should not be any value with a size other than 1, \
  202. 4 or 8. */ \
  203. abort (); \
  204. \
  205. asm volatile ("movq %q0,%%fs:%P1" : \
  206. : IMM_MODE ((unsigned long int) value), \
  207. "i" (offsetof (struct pthread, member))); \
  208. }})
  209. /* Set member of the thread descriptor directly. */
  210. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  211. ({ if (sizeof (descr->member[0]) == 1) \
  212. asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
  213. : "iq" (value), \
  214. "i" (offsetof (struct pthread, member[0])), \
  215. "r" (idx)); \
  216. else if (sizeof (descr->member[0]) == 4) \
  217. asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
  218. : IMM_MODE (value), \
  219. "i" (offsetof (struct pthread, member[0])), \
  220. "r" (idx)); \
  221. else \
  222. { \
  223. if (sizeof (descr->member[0]) != 8) \
  224. /* There should not be any value with a size other than 1, \
  225. 4 or 8. */ \
  226. abort (); \
  227. \
  228. asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
  229. : IMM_MODE ((unsigned long int) value), \
  230. "i" (offsetof (struct pthread, member[0])), \
  231. "r" (idx)); \
  232. }})
  233. /* Atomic compare and exchange on TLS, returning old value. */
  234. #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
  235. ({ __typeof (descr->member) __ret; \
  236. __typeof (oldval) __old = (oldval); \
  237. if (sizeof (descr->member) == 4) \
  238. asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
  239. : "=a" (__ret) \
  240. : "0" (__old), "r" (newval), \
  241. "i" (offsetof (struct pthread, member))); \
  242. else \
  243. /* Not necessary for other sizes in the moment. */ \
  244. abort (); \
  245. __ret; })
  246. /* Atomic set bit. */
  247. #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
  248. (void) ({ if (sizeof ((descr)->member) == 4) \
  249. asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
  250. :: "i" (offsetof (struct pthread, member)), \
  251. "ir" (1 << (bit))); \
  252. else \
  253. /* Not necessary for other sizes in the moment. */ \
  254. abort (); })
  255. #define CALL_THREAD_FCT(descr) \
  256. ({ void *__res; \
  257. asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
  258. "callq *%%fs:%P1" \
  259. : "=a" (__res) \
  260. : "i" (offsetof (struct pthread, start_routine)), \
  261. "i" (offsetof (struct pthread, arg)) \
  262. : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
  263. "memory", "cc"); \
  264. __res; })
  265. #endif /* __ASSEMBLER__ */
  266. #endif /* tls.h */