tls.h 11 KB

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