tls.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Definition for thread-local data handling. NPTL/SH version.
  2. Copyright (C) 2003-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
  17. # include <dl-sysdep.h>
  18. #ifndef __ASSEMBLER__
  19. # include <stdbool.h>
  20. # include <stddef.h>
  21. # include <stdint.h>
  22. # include <stdlib.h>
  23. # include <list.h>
  24. # include <sysdep.h>
  25. # include <dl-dtv.h>
  26. typedef struct
  27. {
  28. dtv_t *dtv;
  29. uintptr_t pointer_guard;
  30. } tcbhead_t;
  31. # define TLS_MULTIPLE_THREADS_IN_TCB 1
  32. #else /* __ASSEMBLER__ */
  33. # include <tcb-offsets.h>
  34. #endif /* __ASSEMBLER__ */
  35. #ifndef __ASSEMBLER__
  36. /* Get system call information. */
  37. # include <sysdep.h>
  38. /* This is the size of the initial TCB. */
  39. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  40. /* Alignment requirements for the initial TCB. */
  41. # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
  42. /* This is the size of the TCB. */
  43. # define TLS_TCB_SIZE sizeof (tcbhead_t)
  44. /* This is the size we need before TCB. */
  45. # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
  46. /* Alignment requirements for the TCB. */
  47. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  48. /* The TLS blocks start right after the TCB. */
  49. # define TLS_DTV_AT_TP 1
  50. # define TLS_TCB_AT_TP 0
  51. /* Get the thread descriptor definition. */
  52. # include <nptl/descr.h>
  53. /* Install the dtv pointer. The pointer passed is to the element with
  54. index -1 which contain the length. */
  55. # define INSTALL_DTV(tcbp, dtvp) \
  56. ((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1
  57. /* Install new dtv for current thread. */
  58. # define INSTALL_NEW_DTV(dtv) \
  59. ({ tcbhead_t *__tcbp; \
  60. __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
  61. __tcbp->dtv = (dtv);})
  62. /* Return dtv of given thread descriptor. */
  63. # define GET_DTV(tcbp) \
  64. (((tcbhead_t *) (tcbp))->dtv)
  65. /* Code to initially initialize the thread pointer. This might need
  66. special attention since 'errno' is not yet available and if the
  67. operation can cause a failure 'errno' must not be touched. */
  68. # define TLS_INIT_TP(tcbp) \
  69. ({ __asm __volatile ("ldc %0,gbr" : : "r" (tcbp)); NULL; })
  70. # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1
  71. /* Return the address of the dtv for the current thread. */
  72. # define THREAD_DTV() \
  73. ({ tcbhead_t *__tcbp; \
  74. __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
  75. __tcbp->dtv;})
  76. /* Return the thread descriptor for the current thread.
  77. The contained asm must *not* be marked volatile since otherwise
  78. assignments like
  79. struct pthread *self = thread_self();
  80. do not get optimized away. */
  81. # define THREAD_SELF \
  82. ({ struct pthread *__self; \
  83. __asm ("stc gbr,%0" : "=r" (__self)); \
  84. __self - 1;})
  85. /* Magic for libthread_db to know how to do THREAD_SELF. */
  86. # define DB_THREAD_SELF \
  87. REGISTER (32, 32, REG_GBR * 4, -sizeof (struct pthread))
  88. /* Read member of the thread descriptor directly. */
  89. # define THREAD_GETMEM(descr, member) (descr->member)
  90. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  91. # define THREAD_GETMEM_NC(descr, member, idx) (descr->member[idx])
  92. /* Set member of the thread descriptor directly. */
  93. # define THREAD_SETMEM(descr, member, value) \
  94. descr->member = (value)
  95. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  96. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  97. descr->member[idx] = (value)
  98. #define THREAD_GET_POINTER_GUARD() \
  99. ({ tcbhead_t *__tcbp; \
  100. __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
  101. __tcbp->pointer_guard;})
  102. #define THREAD_SET_POINTER_GUARD(value) \
  103. ({ tcbhead_t *__tcbp; \
  104. __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
  105. __tcbp->pointer_guard = (value);})
  106. #define THREAD_COPY_POINTER_GUARD(descr) \
  107. ({ tcbhead_t *__tcbp; \
  108. __asm __volatile ("stc gbr,%0" : "=r" (__tcbp)); \
  109. ((tcbhead_t *) (descr + 1))->pointer_guard = __tcbp->pointer_guard;})
  110. /* Get and set the global scope generation counter in struct pthread. */
  111. #define THREAD_GSCOPE_IN_TCB 1
  112. #define THREAD_GSCOPE_FLAG_UNUSED 0
  113. #define THREAD_GSCOPE_FLAG_USED 1
  114. #define THREAD_GSCOPE_FLAG_WAIT 2
  115. #define THREAD_GSCOPE_RESET_FLAG() \
  116. do \
  117. { int __res \
  118. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  119. THREAD_GSCOPE_FLAG_UNUSED); \
  120. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  121. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  122. } \
  123. while (0)
  124. #define THREAD_GSCOPE_SET_FLAG() \
  125. do \
  126. { \
  127. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  128. atomic_write_barrier (); \
  129. } \
  130. while (0)
  131. #define THREAD_GSCOPE_WAIT() \
  132. GL(dl_wait_lookup_done) ()
  133. #endif /* __ASSEMBLER__ */
  134. #endif /* tls.h */