tls.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Definition for thread-local data handling. NPTL/Nios II version.
  2. Copyright (C) 2012-2016 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. #else /* __ASSEMBLER__ */
  32. # include <tcb-offsets.h>
  33. #endif /* __ASSEMBLER__ */
  34. /* We require TLS support in the tools. */
  35. #define HAVE_TLS_SUPPORT 1
  36. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  37. #define HAVE___THREAD 1
  38. /* Signal that TLS support is available. */
  39. #define USE_TLS 1
  40. #ifndef __ASSEMBLER__
  41. /* Get system call information. */
  42. # include <sysdep.h>
  43. /* The TP points to the start of the thread blocks. */
  44. # define TLS_DTV_AT_TP 1
  45. /* Get the thread descriptor definition. */
  46. # include <../../descr.h>
  47. typedef struct
  48. {
  49. dtv_t *dtv;
  50. uintptr_t pointer_guard;
  51. unsigned spare[6];
  52. } tcbhead_t;
  53. register struct pthread *__thread_self __asm__("r23");
  54. #define READ_THREAD_POINTER() ((void *) __thread_self)
  55. /* This is the size of the initial TCB. Because our TCB is before the thread
  56. pointer, we don't need this. */
  57. # define TLS_INIT_TCB_SIZE 0
  58. /* Alignment requirements for the initial TCB. */
  59. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  60. /* This is the size of the TCB. Because our TCB is before the thread
  61. pointer, we don't need this. */
  62. # define TLS_TCB_SIZE 0
  63. /* Alignment requirements for the TCB. */
  64. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  65. /* This is the size we need before TCB - actually, it includes the TCB. */
  66. # define TLS_PRE_TCB_SIZE \
  67. (sizeof (struct pthread) \
  68. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  69. /* The thread pointer (in hardware register r23) points to the end of
  70. the TCB + 0x7000, as for PowerPC and MIPS. */
  71. # define TLS_TCB_OFFSET 0x7000
  72. /* Install the dtv pointer. The pointer passed is to the element with
  73. index -1 which contain the length. */
  74. # define INSTALL_DTV(tcbp, dtvp) \
  75. (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
  76. /* Install new dtv for current thread. */
  77. # define INSTALL_NEW_DTV(dtv) \
  78. (THREAD_DTV() = (dtv))
  79. /* Return dtv of given thread descriptor. */
  80. # define GET_DTV(tcbp) \
  81. (((tcbhead_t *) (tcbp))[-1].dtv)
  82. /* Code to initially initialize the thread pointer. */
  83. # define TLS_INIT_TP(tcbp, secondcall) \
  84. (__thread_self = (struct pthread *) ((char *) tcbp + TLS_TCB_OFFSET), NULL)
  85. /* Value passed to 'clone' for initialization of the thread register. */
  86. # define TLS_DEFINE_INIT_TP(tp, pd) \
  87. void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
  88. /* Return the address of the dtv for the current thread. */
  89. # define THREAD_DTV() \
  90. (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
  91. /* Return the thread descriptor for the current thread. */
  92. # define THREAD_SELF \
  93. ((struct pthread *) (READ_THREAD_POINTER () \
  94. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  95. /* Magic for libthread_db to know how to do THREAD_SELF. */
  96. # define DB_THREAD_SELF \
  97. REGISTER (32, 32, 23 * 4, -TLS_PRE_TCB_SIZE - TLS_TCB_OFFSET)
  98. /* Access to data in the thread descriptor is easy. */
  99. # define THREAD_GETMEM(descr, member) \
  100. descr->member
  101. # define THREAD_GETMEM_NC(descr, member, idx) \
  102. descr->member[idx]
  103. # define THREAD_SETMEM(descr, member, value) \
  104. descr->member = (value)
  105. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  106. descr->member[idx] = (value)
  107. # define THREAD_GET_POINTER_GUARD() \
  108. (((tcbhead_t *) (READ_THREAD_POINTER () \
  109. - TLS_TCB_OFFSET))[-1].pointer_guard)
  110. # define THREAD_SET_POINTER_GUARD(value) \
  111. (THREAD_GET_POINTER_GUARD () = (value))
  112. # define THREAD_COPY_POINTER_GUARD(descr) \
  113. (((tcbhead_t *) ((void *) (descr) \
  114. + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
  115. = THREAD_GET_POINTER_GUARD())
  116. /* l_tls_offset == 0 is perfectly valid on Nios II, so we have to use some
  117. different value to mean unset l_tls_offset. */
  118. # define NO_TLS_OFFSET -1
  119. /* Get and set the global scope generation counter in struct pthread. */
  120. #define THREAD_GSCOPE_FLAG_UNUSED 0
  121. #define THREAD_GSCOPE_FLAG_USED 1
  122. #define THREAD_GSCOPE_FLAG_WAIT 2
  123. #define THREAD_GSCOPE_RESET_FLAG() \
  124. do \
  125. { int __res \
  126. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  127. THREAD_GSCOPE_FLAG_UNUSED); \
  128. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  129. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  130. } \
  131. while (0)
  132. #define THREAD_GSCOPE_SET_FLAG() \
  133. do \
  134. { \
  135. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  136. atomic_write_barrier (); \
  137. } \
  138. while (0)
  139. #define THREAD_GSCOPE_WAIT() \
  140. GL(dl_wait_lookup_done) ()
  141. #endif /* __ASSEMBLER__ */
  142. #endif /* tls.h */