tls.h 16 KB

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