tls.h 14 KB

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