tls.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Definition for thread-local data handling. NPTL/SH 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
  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. dtv_t *dtv;
  38. uintptr_t pointer_guard;
  39. } tcbhead_t;
  40. # define TLS_MULTIPLE_THREADS_IN_TCB 1
  41. #else /* __ASSEMBLER__ */
  42. # include <tcb-offsets.h>
  43. #endif /* __ASSEMBLER__ */
  44. /* We require TLS support in the tools. */
  45. #define HAVE_TLS_SUPPORT
  46. #define HAVE___THREAD 1
  47. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  48. /* Signal that TLS support is available. */
  49. # define USE_TLS 1
  50. #ifndef __ASSEMBLER__
  51. /* Get system call information. */
  52. # include <sysdep.h>
  53. /* This is the size of the initial TCB. */
  54. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  55. /* Alignment requirements for the initial TCB. */
  56. # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
  57. /* This is the size of the TCB. */
  58. # define TLS_TCB_SIZE sizeof (tcbhead_t)
  59. /* This is the size we need before TCB. */
  60. # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
  61. /* Alignment requirements for the TCB. */
  62. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  63. /* The TLS blocks start right after the TCB. */
  64. # define TLS_DTV_AT_TP 1
  65. /* Get the thread descriptor definition. */
  66. # include <descr.h>
  67. /* Install the dtv pointer. The pointer passed is to the element with
  68. index -1 which contain the length. */
  69. # define INSTALL_DTV(tcbp, dtvp) \
  70. ((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1
  71. /* Install new dtv for current thread. */
  72. # define INSTALL_NEW_DTV(dtv) \
  73. ({ tcbhead_t *__tcbp; \
  74. __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
  75. __tcbp->dtv = (dtv);})
  76. /* Return dtv of given thread descriptor. */
  77. # define GET_DTV(tcbp) \
  78. (((tcbhead_t *) (tcbp))->dtv)
  79. /* Code to initially initialize the thread pointer. This might need
  80. special attention since 'errno' is not yet available and if the
  81. operation can cause a failure 'errno' must not be touched. */
  82. # define TLS_INIT_TP(tcbp, secondcall) \
  83. ({ __asm__ __volatile__ ("ldc %0,gbr" : : "r" (tcbp)); 0; })
  84. /* Return the address of the dtv for the current thread. */
  85. # define THREAD_DTV() \
  86. ({ tcbhead_t *__tcbp; \
  87. __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
  88. __tcbp->dtv;})
  89. /* Return the thread descriptor for the current thread.
  90. The contained asm must *not* be marked volatile since otherwise
  91. assignments like
  92. struct pthread *self = thread_self();
  93. do not get optimized away. */
  94. # define THREAD_SELF \
  95. ({ struct pthread *__self; \
  96. __asm__ ("stc gbr,%0" : "=r" (__self)); \
  97. __self - 1;})
  98. /* Magic for libthread_db to know how to do THREAD_SELF. */
  99. # define DB_THREAD_SELF \
  100. REGISTER (32, 32, REG_GBR * 4, -sizeof (struct pthread))
  101. /* Read member of the thread descriptor directly. */
  102. # define THREAD_GETMEM(descr, member) (descr->member)
  103. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  104. # define THREAD_GETMEM_NC(descr, member, idx) (descr->member[idx])
  105. /* Set member of the thread descriptor directly. */
  106. # define THREAD_SETMEM(descr, member, value) \
  107. descr->member = (value)
  108. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  109. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  110. descr->member[idx] = (value)
  111. #define THREAD_GET_POINTER_GUARD() \
  112. ({ tcbhead_t *__tcbp; \
  113. __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
  114. __tcbp->pointer_guard;})
  115. #define THREAD_SET_POINTER_GUARD(value) \
  116. ({ tcbhead_t *__tcbp; \
  117. __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
  118. __tcbp->pointer_guard = (value);})
  119. #define THREAD_COPY_POINTER_GUARD(descr) \
  120. ({ tcbhead_t *__tcbp; \
  121. __asm__ __volatile__ ("stc gbr,%0" : "=r" (__tcbp)); \
  122. ((tcbhead_t *) (descr + 1))->pointer_guard = __tcbp->pointer_guard;})
  123. /* Get and set the global scope generation counter in struct pthread. */
  124. #define THREAD_GSCOPE_FLAG_UNUSED 0
  125. #define THREAD_GSCOPE_FLAG_USED 1
  126. #define THREAD_GSCOPE_FLAG_WAIT 2
  127. #define THREAD_GSCOPE_RESET_FLAG() \
  128. do \
  129. { int __res \
  130. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  131. THREAD_GSCOPE_FLAG_UNUSED); \
  132. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  133. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  134. } \
  135. while (0)
  136. #define THREAD_GSCOPE_SET_FLAG() \
  137. do \
  138. { \
  139. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  140. atomic_write_barrier (); \
  141. } \
  142. while (0)
  143. #define THREAD_GSCOPE_WAIT() \
  144. GL(dl_wait_lookup_done) ()
  145. #endif /* __ASSEMBLER__ */
  146. #endif /* tls.h */