tls.h 7.2 KB

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