tls.h 5.7 KB

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