tls.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Definition for thread-local data handling. NPTL/RISCV64 version.
  2. Copyright (C) 2005, 2007, 2011 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. typedef struct
  32. {
  33. dtv_t *dtv;
  34. } tcbhead_t;
  35. register tcbhead_t *__thread_self __asm__("tp");
  36. # define TLS_MULTIPLE_THREADS_IN_TCB 1
  37. #else /* __ASSEMBLER__ */
  38. # include <tcb-offsets.h>
  39. #endif /* __ASSEMBLER__ */
  40. /* We require TLS support in the tools. */
  41. #define HAVE_TLS_SUPPORT 1
  42. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  43. #define HAVE___THREAD 1
  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 TLS block.
  50. * As I understand it, this isn't strictly that "TP points to DTV" - it's
  51. * more where to place the TCB in the TLS block. This will place it in
  52. * the beginning.
  53. *
  54. * Layout:
  55. * ------------------------------------
  56. * | PRE | TCB | TLS MEMORY .. |
  57. * ------------------------------------
  58. * ^ x4 / TP
  59. *
  60. * PRE is the struct pthread described below
  61. * TCB is tcbhead_t
  62. * TLS memory is where the TLS program sections are loaded
  63. *
  64. * See _dl_allocate_tls_storage and __libc_setup_tls for more information.
  65. */
  66. # define TLS_DTV_AT_TP 1
  67. /* Get the thread descriptor definition. */
  68. # include <../../descr.h>
  69. /* Requirements for the TCB. */
  70. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  71. # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
  72. # define TLS_TCB_SIZE sizeof (tcbhead_t)
  73. # define TLS_TCB_ALIGN __alignof__ (tcbhead_t)
  74. /* This is the size of the TCB. */
  75. /* This is the size we need before TCB.
  76. * To support THREAD_GETMEM with friends we want to have a
  77. * struct pthread available.
  78. * Yank it in infront of everything, I'm sure nobody will mind.
  79. *
  80. * This memory is really allocated PRE the TLS block, so it's possible
  81. * to do ((char*)tlsblock) - TLS_PRE_TCB_SIZE to access it.
  82. * This is done for THREAD_SELF. */
  83. # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
  84. /* Install the dtv pointer.
  85. * When called, dtvp is a pointer not the DTV per say (which should start
  86. * with the generation counter) but to the length of the DTV.
  87. * We can always index with -1, so we store dtvp[1]
  88. */
  89. # define INSTALL_DTV(tcbp, dtvp) \
  90. (((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1)
  91. /* Install new dtv for current thread
  92. * In a logicial world dtv here would also point to the length of the DTV.
  93. * However it does not, this time it points to the generation counter,
  94. * so just store it.
  95. *
  96. * Note: -1 is still valid and contains the length. */
  97. # define INSTALL_NEW_DTV(dtv) \
  98. (THREAD_DTV() = (dtv))
  99. /* Return dtv of given thread descriptor. */
  100. # define GET_DTV(tcbp) \
  101. (((tcbhead_t *) (tcbp))->dtv)
  102. /* Code to initially initialize the thread pointer.
  103. *
  104. * Set TP to the address _after_ tcbhead_t. This will allow us
  105. * to change the size of tcbhead_t without having to re-link everything.
  106. *
  107. * secondcall has something to do with USE__THREAD,
  108. * seems to always be 0 so we don't care about it.
  109. *
  110. * This has to return NULL on success (or a string with the failure text).
  111. * It's hard to fail this, so return NULL always.
  112. */
  113. # define TLS_INIT_TP(tcbp, secondcall) \
  114. ({__thread_self = ((tcbhead_t *)tcbp + 1); NULL;})
  115. /* Return the address of the dtv for the current thread.
  116. *
  117. * Dereference TP, offset to dtv - really straightforward.
  118. * Remember that we made TP point to after tcb, so we need to reverse that.
  119. */
  120. # define THREAD_DTV() \
  121. ((((tcbhead_t *)__thread_self)-1)->dtv)
  122. /* Return the thread descriptor for the current thread.
  123. *
  124. * Return a pointer to the TLS_PRE area where we allocated space for
  125. * a struct pthread. Again, TP points to after tcbhead_t, compensate with
  126. * TLS_INIT_TCB_SIZE.
  127. *
  128. * I regard this is a seperate system from the "normal" TLS.
  129. */
  130. # define THREAD_SELF \
  131. ((struct pthread *) ((char *) __thread_self - TLS_INIT_TCB_SIZE \
  132. - TLS_PRE_TCB_SIZE))
  133. /* Magic for libthread_db to know how to do THREAD_SELF. */
  134. # define DB_THREAD_SELF \
  135. CONST_THREAD_AREA (32, sizeof (struct pthread))
  136. /* Access to data in the thread descriptor is easy. */
  137. #define THREAD_GETMEM(descr, member) \
  138. descr->member
  139. #define THREAD_GETMEM_NC(descr, member, idx) \
  140. descr->member[idx]
  141. #define THREAD_SETMEM(descr, member, value) \
  142. descr->member = (value)
  143. #define THREAD_SETMEM_NC(descr, member, idx, value) \
  144. descr->member[idx] = (value)
  145. /* Get and set the global scope generation counter in struct pthread. */
  146. #define THREAD_GSCOPE_FLAG_UNUSED 0
  147. #define THREAD_GSCOPE_FLAG_USED 1
  148. #define THREAD_GSCOPE_FLAG_WAIT 2
  149. #define THREAD_GSCOPE_RESET_FLAG() \
  150. do \
  151. { int __res \
  152. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  153. THREAD_GSCOPE_FLAG_UNUSED); \
  154. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  155. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  156. } \
  157. while (0)
  158. #define THREAD_GSCOPE_SET_FLAG() \
  159. do \
  160. { \
  161. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  162. atomic_write_barrier (); \
  163. } \
  164. while (0)
  165. #define THREAD_GSCOPE_WAIT() \
  166. GL(dl_wait_lookup_done) ()
  167. #endif /* __ASSEMBLER__ */
  168. #endif /* tls.h */