tls.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /* Definition for thread-local data handling. nptl/i386 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 <dl-sysdep.h>
  19. #ifndef __ASSEMBLER__
  20. # include <stdbool.h>
  21. # include <stddef.h>
  22. # include <stdint.h>
  23. # include <stdlib.h>
  24. # include <list.h>
  25. /* Type for the dtv. */
  26. typedef union dtv
  27. {
  28. size_t counter;
  29. struct
  30. {
  31. void *val;
  32. bool is_static;
  33. } pointer;
  34. } dtv_t;
  35. typedef struct
  36. {
  37. void *tcb; /* Pointer to the TCB. Not necessarily the
  38. thread descriptor used by libpthread. */
  39. dtv_t *dtv;
  40. void *self; /* Pointer to the thread descriptor. */
  41. int multiple_threads;
  42. uintptr_t sysinfo;
  43. } tcbhead_t;
  44. # define TLS_MULTIPLE_THREADS_IN_TCB 1
  45. #else /* __ASSEMBLER__ */
  46. # include <tcb-offsets.h>
  47. #endif
  48. /* We require TLS support in the tools. */
  49. #ifndef HAVE_TLS_SUPPORT
  50. # error "TLS support is required."
  51. #endif
  52. /* Signal that TLS support is available. */
  53. #define USE_TLS 1
  54. /* Alignment requirement for the stack. For IA-32 this is governed by
  55. the SSE memory functions. */
  56. #define STACK_ALIGN 16
  57. #ifndef __ASSEMBLER__
  58. /* Get system call information. */
  59. # include <sysdep.h>
  60. /* The old way: using LDT. */
  61. /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
  62. struct user_desc
  63. {
  64. unsigned int entry_number;
  65. unsigned long int base_addr;
  66. unsigned int limit;
  67. unsigned int seg_32bit:1;
  68. unsigned int contents:2;
  69. unsigned int read_exec_only:1;
  70. unsigned int limit_in_pages:1;
  71. unsigned int seg_not_present:1;
  72. unsigned int useable:1;
  73. unsigned int empty:25;
  74. };
  75. /* Initializing bit fields is slow. We speed it up by using a union. */
  76. union user_desc_init
  77. {
  78. struct user_desc desc;
  79. unsigned int vals[4];
  80. };
  81. /* Get the thread descriptor definition. */
  82. # include <nptl/descr.h>
  83. /* This is the size of the initial TCB. */
  84. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  85. /* Alignment requirements for the initial TCB. */
  86. # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
  87. /* This is the size of the TCB. */
  88. # define TLS_TCB_SIZE sizeof (struct pthread)
  89. /* Alignment requirements for the TCB. */
  90. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  91. /* The TCB can have any size and the memory following the address the
  92. thread pointer points to is unspecified. Allocate the TCB there. */
  93. # define TLS_TCB_AT_TP 1
  94. /* Install the dtv pointer. The pointer passed is to the element with
  95. index -1 which contain the length. */
  96. # define INSTALL_DTV(descr, dtvp) \
  97. ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
  98. /* Install new dtv for current thread. */
  99. # define INSTALL_NEW_DTV(dtvp) \
  100. ({ struct pthread *__pd; \
  101. THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
  102. /* Return dtv of given thread descriptor. */
  103. # define GET_DTV(descr) \
  104. (((tcbhead_t *) (descr))->dtv)
  105. #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
  106. #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
  107. /* Macros to load from and store into segment registers. */
  108. # ifndef TLS_GET_GS
  109. # define TLS_GET_GS() \
  110. ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
  111. # endif
  112. # ifndef TLS_SET_GS
  113. # define TLS_SET_GS(val) \
  114. __asm ("movw %w0, %%gs" :: "q" (val))
  115. # endif
  116. # ifndef __NR_set_thread_area
  117. # define __NR_set_thread_area 243
  118. # endif
  119. # ifndef TLS_FLAG_WRITABLE
  120. # define TLS_FLAG_WRITABLE 0x00000001
  121. # endif
  122. // XXX Enable for the real world.
  123. #if 0
  124. # ifndef __ASSUME_SET_THREAD_AREA
  125. # error "we need set_thread_area"
  126. # endif
  127. #endif
  128. # ifdef __PIC__
  129. # define TLS_EBX_ARG "r"
  130. # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
  131. # else
  132. # define TLS_EBX_ARG "b"
  133. # define TLS_LOAD_EBX
  134. # endif
  135. #if defined NEED_DL_SYSINFO
  136. # define INIT_SYSINFO \
  137. _head->sysinfo = GLRO(dl_sysinfo)
  138. #else
  139. # define INIT_SYSINFO
  140. #endif
  141. #ifndef LOCK_PREFIX
  142. # ifdef UP
  143. # define LOCK_PREFIX /* nothing */
  144. # else
  145. # define LOCK_PREFIX "lock;"
  146. # endif
  147. #endif
  148. /* Code to initially initialize the thread pointer. This might need
  149. special attention since 'errno' is not yet available and if the
  150. operation can cause a failure 'errno' must not be touched. */
  151. # define TLS_INIT_TP(thrdescr, secondcall) \
  152. ({ void *_thrdescr = (thrdescr); \
  153. tcbhead_t *_head = _thrdescr; \
  154. union user_desc_init _segdescr; \
  155. int _result; \
  156. \
  157. _head->tcb = _thrdescr; \
  158. /* For now the thread descriptor is at the same address. */ \
  159. _head->self = _thrdescr; \
  160. /* New syscall handling support. */ \
  161. INIT_SYSINFO; \
  162. \
  163. /* The 'entry_number' field. Let the kernel pick a value. */ \
  164. if (secondcall) \
  165. _segdescr.vals[0] = TLS_GET_GS () >> 3; \
  166. else \
  167. _segdescr.vals[0] = -1; \
  168. /* The 'base_addr' field. Pointer to the TCB. */ \
  169. _segdescr.vals[1] = (unsigned long int) _thrdescr; \
  170. /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
  171. _segdescr.vals[2] = 0xfffff; \
  172. /* Collapsed value of the bitfield: \
  173. .seg_32bit = 1 \
  174. .contents = 0 \
  175. .read_exec_only = 0 \
  176. .limit_in_pages = 1 \
  177. .seg_not_present = 0 \
  178. .useable = 1 */ \
  179. _segdescr.vals[3] = 0x51; \
  180. \
  181. /* Install the TLS. */ \
  182. asm volatile (TLS_LOAD_EBX \
  183. "int $0x80\n\t" \
  184. TLS_LOAD_EBX \
  185. : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
  186. : "0" (__NR_set_thread_area), \
  187. TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
  188. \
  189. if (_result == 0) \
  190. /* We know the index in the GDT, now load the segment register. \
  191. The use of the GDT is described by the value 3 in the lower \
  192. three bits of the segment descriptor value. \
  193. \
  194. Note that we have to do this even if the numeric value of \
  195. the descriptor does not change. Loading the segment register \
  196. causes the segment information from the GDT to be loaded \
  197. which is necessary since we have changed it. */ \
  198. TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
  199. \
  200. _result == 0 ? NULL \
  201. : "set_thread_area failed when setting up thread-local storage\n"; })
  202. /* Return the address of the dtv for the current thread. */
  203. # define THREAD_DTV() \
  204. ({ struct pthread *__pd; \
  205. THREAD_GETMEM (__pd, header.dtv); })
  206. /* Return the thread descriptor for the current thread.
  207. The contained asm must *not* be marked volatile since otherwise
  208. assignments like
  209. pthread_descr self = thread_self();
  210. do not get optimized away. */
  211. # define THREAD_SELF \
  212. ({ struct pthread *__self; \
  213. asm ("movl %%gs:%c1,%0" : "=r" (__self) \
  214. : "i" (offsetof (struct pthread, header.self))); \
  215. __self;})
  216. /* Magic for libthread_db to know how to do THREAD_SELF. */
  217. # define DB_THREAD_SELF \
  218. REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
  219. REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
  220. /* Read member of the thread descriptor directly. */
  221. # define THREAD_GETMEM(descr, member) \
  222. ({ __typeof (descr->member) __value; \
  223. if (sizeof (__value) == 1) \
  224. asm volatile ("movb %%gs:%P2,%b0" \
  225. : "=q" (__value) \
  226. : "0" (0), "i" (offsetof (struct pthread, member))); \
  227. else if (sizeof (__value) == 4) \
  228. asm volatile ("movl %%gs:%P1,%0" \
  229. : "=r" (__value) \
  230. : "i" (offsetof (struct pthread, member))); \
  231. else \
  232. { \
  233. if (sizeof (__value) != 8) \
  234. /* There should not be any value with a size other than 1, \
  235. 4 or 8. */ \
  236. abort (); \
  237. \
  238. asm volatile ("movl %%gs:%P1,%%eax\n\t" \
  239. "movl %%gs:%P2,%%edx" \
  240. : "=A" (__value) \
  241. : "i" (offsetof (struct pthread, member)), \
  242. "i" (offsetof (struct pthread, member) + 4)); \
  243. } \
  244. __value; })
  245. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  246. # define THREAD_GETMEM_NC(descr, member, idx) \
  247. ({ __typeof (descr->member[0]) __value; \
  248. if (sizeof (__value) == 1) \
  249. asm volatile ("movb %%gs:%P2(%3),%b0" \
  250. : "=q" (__value) \
  251. : "0" (0), "i" (offsetof (struct pthread, member[0])), \
  252. "r" (idx)); \
  253. else if (sizeof (__value) == 4) \
  254. asm volatile ("movl %%gs:%P1(,%2,4),%0" \
  255. : "=r" (__value) \
  256. : "i" (offsetof (struct pthread, member[0])), \
  257. "r" (idx)); \
  258. else \
  259. { \
  260. if (sizeof (__value) != 8) \
  261. /* There should not be any value with a size other than 1, \
  262. 4 or 8. */ \
  263. abort (); \
  264. \
  265. asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
  266. "movl %%gs:4+%P1(,%2,8),%%edx" \
  267. : "=&A" (__value) \
  268. : "i" (offsetof (struct pthread, member[0])), \
  269. "r" (idx)); \
  270. } \
  271. __value; })
  272. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  273. # define THREAD_SETMEM(descr, member, value) \
  274. ({ if (sizeof (descr->member) == 1) \
  275. asm volatile ("movb %b0,%%gs:%P1" : \
  276. : "iq" (value), \
  277. "i" (offsetof (struct pthread, member))); \
  278. else if (sizeof (descr->member) == 4) \
  279. asm volatile ("movl %0,%%gs:%P1" : \
  280. : "ir" (value), \
  281. "i" (offsetof (struct pthread, member))); \
  282. else \
  283. { \
  284. if (sizeof (descr->member) != 8) \
  285. /* There should not be any value with a size other than 1, \
  286. 4 or 8. */ \
  287. abort (); \
  288. \
  289. asm volatile ("movl %%eax,%%gs:%P1\n\t" \
  290. "movl %%edx,%%gs:%P2" : \
  291. : "A" (value), \
  292. "i" (offsetof (struct pthread, member)), \
  293. "i" (offsetof (struct pthread, member) + 4)); \
  294. }})
  295. /* Set member of the thread descriptor directly. */
  296. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  297. ({ if (sizeof (descr->member[0]) == 1) \
  298. asm volatile ("movb %b0,%%gs:%P1(%2)" : \
  299. : "iq" (value), \
  300. "i" (offsetof (struct pthread, member)), \
  301. "r" (idx)); \
  302. else if (sizeof (descr->member[0]) == 4) \
  303. asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
  304. : "ir" (value), \
  305. "i" (offsetof (struct pthread, member)), \
  306. "r" (idx)); \
  307. else \
  308. { \
  309. if (sizeof (descr->member[0]) != 8) \
  310. /* There should not be any value with a size other than 1, \
  311. 4 or 8. */ \
  312. abort (); \
  313. \
  314. asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
  315. "movl %%edx,%%gs:4+%P1(,%2,8)" : \
  316. : "A" (value), \
  317. "i" (offsetof (struct pthread, member)), \
  318. "r" (idx)); \
  319. }})
  320. /* Atomic compare and exchange on TLS, returning old value. */
  321. #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
  322. ({ __typeof (descr->member) __ret; \
  323. __typeof (oldval) __old = (oldval); \
  324. if (sizeof (descr->member) == 4) \
  325. asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
  326. : "=a" (__ret) \
  327. : "0" (__old), "r" (newval), \
  328. "i" (offsetof (struct pthread, member))); \
  329. else \
  330. /* Not necessary for other sizes in the moment. */ \
  331. abort (); \
  332. __ret; })
  333. /* Atomic set bit. */
  334. #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
  335. (void) ({ if (sizeof ((descr)->member) == 4) \
  336. asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
  337. :: "i" (offsetof (struct pthread, member)), \
  338. "ir" (1 << (bit))); \
  339. else \
  340. /* Not necessary for other sizes in the moment. */ \
  341. abort (); })
  342. /* Call the user-provided thread function. */
  343. #define CALL_THREAD_FCT(descr) \
  344. ({ void *__res; \
  345. int __ignore1, __ignore2; \
  346. asm volatile ("pushl %%eax\n\t" \
  347. "pushl %%eax\n\t" \
  348. "pushl %%eax\n\t" \
  349. "pushl %%gs:%P4\n\t" \
  350. "call *%%gs:%P3\n\t" \
  351. "addl $16, %%esp" \
  352. : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
  353. : "i" (offsetof (struct pthread, start_routine)), \
  354. "i" (offsetof (struct pthread, arg))); \
  355. __res; })
  356. #endif /* __ASSEMBLER__ */
  357. #endif /* tls.h */