tls.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Definition for thread-local data handling. NPTL/MIPS version.
  2. Copyright (C) 2005-2019 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. #include <dl-sysdep.h>
  18. #ifndef __ASSEMBLER__
  19. # include <stdbool.h>
  20. # include <stddef.h>
  21. # include <stdint.h>
  22. # include <dl-dtv.h>
  23. /* Get system call information. */
  24. # include <sysdep.h>
  25. #ifdef __mips16
  26. /* MIPS16 uses GCC builtin to access the TP. */
  27. # define READ_THREAD_POINTER() (__builtin_thread_pointer ())
  28. #else
  29. /* Note: rd must be $v1 to be ABI-conformant. */
  30. # if __mips_isa_rev >= 2
  31. # define READ_THREAD_POINTER() \
  32. ({ void *__result; \
  33. asm volatile ("rdhwr\t%0, $29" : "=v" (__result)); \
  34. __result; })
  35. # else
  36. # define READ_THREAD_POINTER() \
  37. ({ void *__result; \
  38. asm volatile (".set\tpush\n\t.set\tmips32r2\n\t" \
  39. "rdhwr\t%0, $29\n\t.set\tpop" : "=v" (__result)); \
  40. __result; })
  41. # endif
  42. #endif
  43. #else /* __ASSEMBLER__ */
  44. # include <tcb-offsets.h>
  45. # if __mips_isa_rev >= 2
  46. # define READ_THREAD_POINTER(rd) rdhwr rd, $29
  47. # else
  48. # define READ_THREAD_POINTER(rd) \
  49. .set push; \
  50. .set mips32r2; \
  51. rdhwr rd, $29; \
  52. .set pop
  53. # endif
  54. #endif /* __ASSEMBLER__ */
  55. #ifndef __ASSEMBLER__
  56. /* The TP points to the start of the thread blocks. */
  57. # define TLS_DTV_AT_TP 1
  58. # define TLS_TCB_AT_TP 0
  59. /* Get the thread descriptor definition. */
  60. # include <nptl/descr.h>
  61. typedef struct
  62. {
  63. dtv_t *dtv;
  64. void *private;
  65. } tcbhead_t;
  66. /* This is the size of the initial TCB. Because our TCB is before the thread
  67. pointer, we don't need this. */
  68. # define TLS_INIT_TCB_SIZE 0
  69. /* Alignment requirements for the initial TCB. */
  70. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  71. /* This is the size of the TCB. Because our TCB is before the thread
  72. pointer, we don't need this. */
  73. # define TLS_TCB_SIZE 0
  74. /* Alignment requirements for the TCB. */
  75. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  76. /* This is the size we need before TCB - actually, it includes the TCB. */
  77. # define TLS_PRE_TCB_SIZE \
  78. (sizeof (struct pthread) \
  79. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  80. /* The thread pointer (in hardware register $29) points to the end of
  81. the TCB + 0x7000, as for PowerPC. The pthread_descr structure is
  82. immediately in front of the TCB. */
  83. # define TLS_TCB_OFFSET 0x7000
  84. /* Install the dtv pointer. The pointer passed is to the element with
  85. index -1 which contain the length. */
  86. # define INSTALL_DTV(tcbp, dtvp) \
  87. (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
  88. /* Install new dtv for current thread. */
  89. # define INSTALL_NEW_DTV(dtv) \
  90. (THREAD_DTV() = (dtv))
  91. /* Return dtv of given thread descriptor. */
  92. # define GET_DTV(tcbp) \
  93. (((tcbhead_t *) (tcbp))[-1].dtv)
  94. /* Code to initially initialize the thread pointer. This might need
  95. special attention since 'errno' is not yet available and if the
  96. operation can cause a failure 'errno' must not be touched. */
  97. # define TLS_INIT_TP(tcbp) \
  98. ({ INTERNAL_SYSCALL_DECL (err); \
  99. long result_var; \
  100. result_var = INTERNAL_SYSCALL (set_thread_area, err, 1, \
  101. (char *) (tcbp) + TLS_TCB_OFFSET); \
  102. INTERNAL_SYSCALL_ERROR_P (result_var, err) \
  103. ? "unknown error" : NULL; })
  104. /* Value passed to 'clone' for initialization of the thread register. */
  105. # define TLS_DEFINE_INIT_TP(tp, pd) \
  106. void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
  107. /* Return the address of the dtv for the current thread. */
  108. # define THREAD_DTV() \
  109. (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
  110. /* Return the thread descriptor for the current thread. */
  111. # define THREAD_SELF \
  112. ((struct pthread *) (READ_THREAD_POINTER () \
  113. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  114. /* Magic for libthread_db to know how to do THREAD_SELF. */
  115. # define DB_THREAD_SELF \
  116. CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
  117. /* Access to data in the thread descriptor is easy. */
  118. # define THREAD_GETMEM(descr, member) \
  119. descr->member
  120. # define THREAD_GETMEM_NC(descr, member, idx) \
  121. descr->member[idx]
  122. # define THREAD_SETMEM(descr, member, value) \
  123. descr->member = (value)
  124. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  125. descr->member[idx] = (value)
  126. /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some
  127. different value to mean unset l_tls_offset. */
  128. # define NO_TLS_OFFSET -1
  129. /* Get and set the global scope generation counter in struct pthread. */
  130. #define THREAD_GSCOPE_IN_TCB 1
  131. #define THREAD_GSCOPE_FLAG_UNUSED 0
  132. #define THREAD_GSCOPE_FLAG_USED 1
  133. #define THREAD_GSCOPE_FLAG_WAIT 2
  134. #define THREAD_GSCOPE_RESET_FLAG() \
  135. do \
  136. { int __res \
  137. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  138. THREAD_GSCOPE_FLAG_UNUSED); \
  139. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  140. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  141. } \
  142. while (0)
  143. #define THREAD_GSCOPE_SET_FLAG() \
  144. do \
  145. { \
  146. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  147. atomic_write_barrier (); \
  148. } \
  149. while (0)
  150. #define THREAD_GSCOPE_WAIT() \
  151. GL(dl_wait_lookup_done) ()
  152. #endif /* __ASSEMBLER__ */
  153. #endif /* tls.h */