tls.h 5.7 KB

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