tls.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* Definition for thread-local data handling. NPTL/PowerPC version.
  2. Copyright (C) 2003, 2005, 2006, 2007 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. /* Type for the dtv. */
  23. typedef union dtv
  24. {
  25. size_t counter;
  26. struct
  27. {
  28. void *val;
  29. bool is_static;
  30. } pointer;
  31. } dtv_t;
  32. #else /* __ASSEMBLER__ */
  33. # include <tcb-offsets.h>
  34. #endif /* __ASSEMBLER__ */
  35. /* We require TLS support in the tools. */
  36. #define HAVE_TLS_SUPPORT 1
  37. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  38. #define HAVE___THREAD 1
  39. /* Signal that TLS support is available. */
  40. #define USE_TLS 1
  41. /* We require TLS support in the tools. */
  42. #ifndef HAVE_TLS_SUPPORT
  43. # error "TLS support is required."
  44. #endif
  45. /* Signal that TLS support is available. */
  46. # define USE_TLS 1
  47. #ifndef __ASSEMBLER__
  48. /* Get system call information. */
  49. # include <sysdep.h>
  50. /* The TP points to the start of the thread blocks. */
  51. # define TLS_DTV_AT_TP 1
  52. /* We use the multiple_threads field in the pthread struct */
  53. #define TLS_MULTIPLE_THREADS_IN_TCB 1
  54. /* Get the thread descriptor definition. */
  55. # include <../../descr.h>
  56. /* The stack_guard is accessed directly by GCC -fstack-protector code,
  57. so it is a part of public ABI. The dtv and pointer_guard fields
  58. are private. */
  59. typedef struct
  60. {
  61. uintptr_t pointer_guard;
  62. uintptr_t stack_guard;
  63. dtv_t *dtv;
  64. } tcbhead_t;
  65. /* This is the size of the initial TCB. */
  66. # define TLS_INIT_TCB_SIZE 0
  67. /* Alignment requirements for the initial TCB. */
  68. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  69. /* This is the size of the TCB. */
  70. # define TLS_TCB_SIZE 0
  71. /* Alignment requirements for the TCB. */
  72. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  73. /* This is the size we need before TCB. */
  74. # define TLS_PRE_TCB_SIZE \
  75. (sizeof (struct pthread) \
  76. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  77. # ifndef __powerpc64__
  78. /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
  79. register void *__thread_register __asm__ ("r2");
  80. # define PT_THREAD_POINTER PT_R2
  81. # else
  82. /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
  83. register void *__thread_register __asm__ ("r13");
  84. # define PT_THREAD_POINTER PT_R13
  85. # endif
  86. /* The following assumes that TP (R2 or R13) points to the end of the
  87. TCB + 0x7000 (per the ABI). This implies that TCB address is
  88. TP - 0x7000. As we define TLS_DTV_AT_TP we can
  89. assume that the pthread struct is allocated immediately ahead of the
  90. TCB. This implies that the pthread_descr address is
  91. TP - (TLS_PRE_TCB_SIZE + 0x7000). */
  92. # define TLS_TCB_OFFSET 0x7000
  93. /* Install the dtv pointer. The pointer passed is to the element with
  94. index -1 which contain the length. */
  95. # define INSTALL_DTV(tcbp, dtvp) \
  96. ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
  97. /* Install new dtv for current thread. */
  98. # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
  99. /* Return dtv of given thread descriptor. */
  100. # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
  101. /* Code to initially initialize the thread pointer. This might need
  102. special attention since 'errno' is not yet available and if the
  103. operation can cause a failure 'errno' must not be touched. */
  104. # define TLS_INIT_TP(tcbp, secondcall) \
  105. (__thread_register = (void *) (tcbp) + TLS_TCB_OFFSET, NULL)
  106. /* Return the address of the dtv for the current thread. */
  107. # define THREAD_DTV() \
  108. (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
  109. /* Return the thread descriptor for the current thread. */
  110. # define THREAD_SELF \
  111. ((struct pthread *) (__thread_register \
  112. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  113. /* Magic for libthread_db to know how to do THREAD_SELF. */
  114. # define DB_THREAD_SELF \
  115. REGISTER (32, 32, PT_THREAD_POINTER * 4, \
  116. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \
  117. REGISTER (64, 64, PT_THREAD_POINTER * 8, \
  118. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
  119. /* Read member of the thread descriptor directly. */
  120. # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
  121. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  122. # define THREAD_GETMEM_NC(descr, member, idx) \
  123. ((void)(descr), (THREAD_SELF)->member[idx])
  124. /* Set member of the thread descriptor directly. */
  125. # define THREAD_SETMEM(descr, member, value) \
  126. ((void)(descr), (THREAD_SELF)->member = (value))
  127. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  128. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  129. ((void)(descr), (THREAD_SELF)->member[idx] = (value))
  130. /* Set the stack guard field in TCB head. */
  131. # define THREAD_SET_STACK_GUARD(value) \
  132. (((tcbhead_t *) ((char *) __thread_register \
  133. - TLS_TCB_OFFSET))[-1].stack_guard = (value))
  134. # define THREAD_COPY_STACK_GUARD(descr) \
  135. (((tcbhead_t *) ((char *) (descr) \
  136. + TLS_PRE_TCB_SIZE))[-1].stack_guard \
  137. = ((tcbhead_t *) ((char *) __thread_register \
  138. - TLS_TCB_OFFSET))[-1].stack_guard)
  139. /* Set the stack guard field in TCB head. */
  140. # define THREAD_GET_POINTER_GUARD() \
  141. (((tcbhead_t *) ((char *) __thread_register \
  142. - TLS_TCB_OFFSET))[-1].pointer_guard)
  143. # define THREAD_SET_POINTER_GUARD(value) \
  144. (THREAD_GET_POINTER_GUARD () = (value))
  145. # define THREAD_COPY_POINTER_GUARD(descr) \
  146. (((tcbhead_t *) ((char *) (descr) \
  147. + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
  148. = THREAD_GET_POINTER_GUARD())
  149. /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
  150. different value to mean unset l_tls_offset. */
  151. # define NO_TLS_OFFSET -1
  152. /* Get and set the global scope generation counter in struct pthread. */
  153. #define THREAD_GSCOPE_FLAG_UNUSED 0
  154. #define THREAD_GSCOPE_FLAG_USED 1
  155. #define THREAD_GSCOPE_FLAG_WAIT 2
  156. #define THREAD_GSCOPE_RESET_FLAG() \
  157. do \
  158. { int __res \
  159. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  160. THREAD_GSCOPE_FLAG_UNUSED); \
  161. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  162. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  163. } \
  164. while (0)
  165. #define THREAD_GSCOPE_SET_FLAG() \
  166. do \
  167. { \
  168. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  169. atomic_write_barrier (); \
  170. } \
  171. while (0)
  172. #define THREAD_GSCOPE_WAIT() \
  173. GL(dl_wait_lookup_done) ()
  174. #endif /* __ASSEMBLER__ */
  175. #endif /* tls.h */