tls.h 15 KB

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