tls.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Definitions for thread-local data handling. NPTL/sparc version.
  2. Copyright (C) 2003, 2005, 2006, 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
  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 <bits/kernel-features.h>
  25. /* Type for the dtv. */
  26. typedef union dtv
  27. {
  28. size_t counter;
  29. struct
  30. {
  31. void *val;
  32. bool is_static;
  33. } pointer;
  34. } dtv_t;
  35. typedef struct
  36. {
  37. void *tcb; /* Pointer to the TCB. Not necessary the
  38. thread descriptor used by libpthread. */
  39. dtv_t *dtv;
  40. void *self;
  41. int multiple_threads;
  42. #if __WORDSIZE == 64
  43. int gscope_flag;
  44. #endif
  45. uintptr_t sysinfo;
  46. uintptr_t stack_guard;
  47. uintptr_t pointer_guard;
  48. #if __WORDSIZE != 64
  49. int gscope_flag;
  50. #endif
  51. #ifndef __ASSUME_PRIVATE_FUTEX
  52. int private_futex;
  53. #endif
  54. } tcbhead_t;
  55. #else /* __ASSEMBLER__ */
  56. # include <tcb-offsets.h>
  57. #endif /* __ASSEMBLER__ */
  58. /* We require TLS support in the tools. */
  59. #define HAVE_TLS_SUPPORT
  60. #define HAVE___THREAD 1
  61. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  62. /* Signal that TLS support is available. */
  63. #define USE_TLS 1
  64. #ifndef __ASSEMBLER__
  65. /* Get system call information. */
  66. # include <sysdep.h>
  67. /* Get the thread descriptor definition. */
  68. # include <descr.h>
  69. register struct pthread *__thread_self __asm__("%g7");
  70. /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
  71. because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
  72. struct pthread even when not linked with -lpthread. */
  73. # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
  74. /* Alignment requirements for the initial TCB. */
  75. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  76. /* This is the size of the TCB. */
  77. # define TLS_TCB_SIZE sizeof (struct pthread)
  78. /* Alignment requirements for the TCB. */
  79. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  80. /* The TCB can have any size and the memory following the address the
  81. thread pointer points to is unspecified. Allocate the TCB there. */
  82. # define TLS_TCB_AT_TP 1
  83. /* Install the dtv pointer. The pointer passed is to the element with
  84. index -1 which contain the length. */
  85. # define INSTALL_DTV(descr, dtvp) \
  86. ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
  87. /* Install new dtv for current thread. */
  88. # define INSTALL_NEW_DTV(DTV) \
  89. (((tcbhead_t *) __thread_self)->dtv = (DTV))
  90. /* Return dtv of given thread descriptor. */
  91. # define GET_DTV(descr) \
  92. (((tcbhead_t *) (descr))->dtv)
  93. /* Code to initially initialize the thread pointer. */
  94. # define TLS_INIT_TP(descr, secondcall) \
  95. (__thread_self = (__typeof (__thread_self)) (descr), NULL)
  96. /* Return the address of the dtv for the current thread. */
  97. # define THREAD_DTV() \
  98. (((tcbhead_t *) __thread_self)->dtv)
  99. /* Return the thread descriptor for the current thread. */
  100. #define THREAD_SELF __thread_self
  101. /* Magic for libthread_db to know how to do THREAD_SELF. */
  102. # define DB_THREAD_SELF_INCLUDE <sys/ucontext.h>
  103. # define DB_THREAD_SELF \
  104. REGISTER (32, 32, REG_G7 * 4, 0) REGISTER (64, 64, REG_G7 * 8, 0)
  105. /* Access to data in the thread descriptor is easy. */
  106. #define THREAD_GETMEM(descr, member) \
  107. descr->member
  108. #define THREAD_GETMEM_NC(descr, member, idx) \
  109. descr->member[idx]
  110. #define THREAD_SETMEM(descr, member, value) \
  111. descr->member = (value)
  112. #define THREAD_SETMEM_NC(descr, member, idx, value) \
  113. descr->member[idx] = (value)
  114. /* Set the stack guard field in TCB head. */
  115. #define THREAD_SET_STACK_GUARD(value) \
  116. THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
  117. # define THREAD_COPY_STACK_GUARD(descr) \
  118. ((descr)->header.stack_guard \
  119. = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
  120. /* Get/set the stack guard field in TCB head. */
  121. #define THREAD_GET_POINTER_GUARD() \
  122. THREAD_GETMEM (THREAD_SELF, header.pointer_guard)
  123. #define THREAD_SET_POINTER_GUARD(value) \
  124. THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
  125. # define THREAD_COPY_POINTER_GUARD(descr) \
  126. ((descr)->header.pointer_guard = THREAD_GET_POINTER_GUARD ())
  127. /* Get and set the global scope generation counter in struct pthread. */
  128. #define THREAD_GSCOPE_FLAG_UNUSED 0
  129. #define THREAD_GSCOPE_FLAG_USED 1
  130. #define THREAD_GSCOPE_FLAG_WAIT 2
  131. #define THREAD_GSCOPE_RESET_FLAG() \
  132. do \
  133. { int __res \
  134. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  135. THREAD_GSCOPE_FLAG_UNUSED); \
  136. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  137. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  138. } \
  139. while (0)
  140. #define THREAD_GSCOPE_SET_FLAG() \
  141. do \
  142. { \
  143. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  144. atomic_write_barrier (); \
  145. } \
  146. while (0)
  147. #define THREAD_GSCOPE_WAIT() \
  148. GL(dl_wait_lookup_done) ()
  149. #endif /* !ASSEMBLER */
  150. #endif /* tls.h */