tls.h 7.0 KB

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