tls.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Definition for thread-local data handling. NPTL/MIPS version.
  2. Copyright (C) 2005, 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 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. /* Note: rd must be $v1 to be ABI-conformant. */
  32. # define READ_THREAD_POINTER() \
  33. ({ void *__result; \
  34. __asm__ __volatile__ (".set\tpush\n\t.set\tmips32r2\n\t" \
  35. "rdhwr\t%0, $29\n\t.set\tpop" : "=v" (__result)); \
  36. __result; })
  37. #else /* __ASSEMBLER__ */
  38. # include <tcb-offsets.h>
  39. # define READ_THREAD_POINTER(rd) \
  40. .set push; \
  41. .set mips32r2; \
  42. rdhwr rd, $29; \
  43. .set pop
  44. #endif /* __ASSEMBLER__ */
  45. /* We require TLS support in the tools. */
  46. #define HAVE_TLS_SUPPORT 1
  47. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  48. #define HAVE___THREAD 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. /* The TP points to the start of the thread blocks. */
  55. # define TLS_DTV_AT_TP 1
  56. /* Get the thread descriptor definition. */
  57. #include <../../descr.h>
  58. typedef struct
  59. {
  60. dtv_t *dtv;
  61. void *private;
  62. } tcbhead_t;
  63. /* This is the size of the initial TCB. Because our TCB is before the thread
  64. pointer, we don't need this. */
  65. # define TLS_INIT_TCB_SIZE 0
  66. /* Alignment requirements for the initial TCB. */
  67. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  68. /* This is the size of the TCB. Because our TCB is before the thread
  69. pointer, we don't need this. */
  70. # define TLS_TCB_SIZE 0
  71. /* Alignment requirements for the TCB. */
  72. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  73. /* This is the size we need before TCB - actually, it includes the TCB. */
  74. # define TLS_PRE_TCB_SIZE \
  75. (sizeof (struct pthread) \
  76. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  77. /* The thread pointer (in hardware register $29) points to the end of
  78. the TCB + 0x7000, as for PowerPC. The pthread_descr structure is
  79. immediately in front of the TCB. */
  80. # define TLS_TCB_OFFSET 0x7000
  81. /* Install the dtv pointer. The pointer passed is to the element with
  82. index -1 which contain the length. */
  83. # define INSTALL_DTV(tcbp, dtvp) \
  84. (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
  85. /* Install new dtv for current thread. */
  86. # define INSTALL_NEW_DTV(dtv) \
  87. (THREAD_DTV() = (dtv))
  88. /* Return dtv of given thread descriptor. */
  89. # define GET_DTV(tcbp) \
  90. (((tcbhead_t *) (tcbp))[-1].dtv)
  91. /* Code to initially initialize the thread pointer. This might need
  92. special attention since 'errno' is not yet available and if the
  93. operation can cause a failure 'errno' must not be touched. */
  94. # define TLS_INIT_TP(tcbp, secondcall) \
  95. ({ INTERNAL_SYSCALL_DECL (err); \
  96. long result_var attribute_unused; \
  97. result_var = INTERNAL_SYSCALL (set_thread_area, err, 1, \
  98. (char *) (tcbp) + TLS_TCB_OFFSET); \
  99. INTERNAL_SYSCALL_ERROR_P (result_var, err) \
  100. ? "unknown error" : NULL; })
  101. /* Return the address of the dtv for the current thread. */
  102. # define THREAD_DTV() \
  103. (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
  104. /* Return the thread descriptor for the current thread. */
  105. # define THREAD_SELF \
  106. ((struct pthread *) (READ_THREAD_POINTER () \
  107. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  108. /* Magic for libthread_db to know how to do THREAD_SELF. */
  109. # define DB_THREAD_SELF \
  110. CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
  111. /* Access to data in the thread descriptor is easy. */
  112. # define THREAD_GETMEM(descr, member) \
  113. descr->member
  114. # define THREAD_GETMEM_NC(descr, member, idx) \
  115. descr->member[idx]
  116. # define THREAD_SETMEM(descr, member, value) \
  117. descr->member = (value)
  118. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  119. descr->member[idx] = (value)
  120. /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some
  121. different value to mean unset l_tls_offset. */
  122. # define NO_TLS_OFFSET -1
  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 */