tls.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Definitions for thread-local data handling. linuxthreads/PPC version.
  2. Copyright (C) 2003, 2005 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 <pt-machine.h>
  20. # include <stdbool.h>
  21. # include <stddef.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. #else /* __ASSEMBLER__ */
  33. # include <tcb-offsets.h>
  34. #endif /* __ASSEMBLER__ */
  35. #ifdef HAVE_TLS_SUPPORT
  36. /* Signal that TLS support is available. */
  37. # define USE_TLS 1
  38. # ifndef __ASSEMBLER__
  39. /* This layout is actually wholly private and not affected by the ABI.
  40. Nor does it overlap the pthread data structure, so we need nothing
  41. extra here at all. */
  42. typedef struct
  43. {
  44. dtv_t *dtv;
  45. } tcbhead_t;
  46. /* This is the size of the initial TCB. */
  47. # define TLS_INIT_TCB_SIZE 0
  48. /* Alignment requirements for the initial TCB. */
  49. # define TLS_INIT_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
  50. /* This is the size of the TCB. */
  51. # define TLS_TCB_SIZE 0
  52. /* Alignment requirements for the TCB. */
  53. # define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
  54. /* This is the size we need before TCB. */
  55. # define TLS_PRE_TCB_SIZE \
  56. (sizeof (struct _pthread_descr_struct) \
  57. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  58. /* The following assumes that TP (R2 or R13) is points to the end of the
  59. TCB + 0x7000 (per the ABI). This implies that TCB address is
  60. TP - 0x7000. As we define TLS_DTV_AT_TP we can
  61. assume that the pthread_descr is allocated immediately ahead of the
  62. TCB. This implies that the pthread_descr address is
  63. TP - (TLS_PRE_TCB_SIZE + 0x7000). */
  64. #define TLS_TCB_OFFSET 0x7000
  65. /* The DTV is allocated at the TP; the TCB is placed elsewhere. */
  66. /* This is not really true for powerpc64. We are following alpha
  67. where the DTV pointer is first doubleword in the TCB. */
  68. # define TLS_DTV_AT_TP 1
  69. /* Install the dtv pointer. The pointer passed is to the element with
  70. index -1 which contain the length. */
  71. # define INSTALL_DTV(TCBP, DTVP) \
  72. (((tcbhead_t *) (TCBP))[-1].dtv = (DTVP) + 1)
  73. /* Install new dtv for current thread. */
  74. # define INSTALL_NEW_DTV(DTV) (THREAD_DTV() = (DTV))
  75. /* Return dtv of given thread descriptor. */
  76. # define GET_DTV(TCBP) (((tcbhead_t *) (TCBP))[-1].dtv)
  77. /* We still need this define so that tcb-offsets.sym can override it and
  78. use THREAD_SELF to generate MULTIPLE_THREADS_OFFSET. */
  79. # define __thread_register ((void *) __thread_self)
  80. /* Code to initially initialize the thread pointer. This might need
  81. special attention since 'errno' is not yet available and if the
  82. operation can cause a failure 'errno' must not be touched.
  83. The global register variable is declared in pt-machine.h with the
  84. wrong type, so we need some extra casts to get the desired result.
  85. This avoids a lvalue cast that gcc-3.4 does not like. */
  86. # define TLS_INIT_TP(TCBP, SECONDCALL) \
  87. (__thread_self = (struct _pthread_descr_struct *) \
  88. ((void *) (TCBP) + TLS_TCB_OFFSET), NULL)
  89. /* Return the address of the dtv for the current thread. */
  90. # define THREAD_DTV() \
  91. (((tcbhead_t *) ((void *) __thread_self - TLS_TCB_OFFSET))[-1].dtv)
  92. /* Return the thread descriptor for the current thread. */
  93. # undef THREAD_SELF
  94. # define THREAD_SELF \
  95. ((pthread_descr) (__thread_register \
  96. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  97. # undef INIT_THREAD_SELF
  98. # define INIT_THREAD_SELF(DESCR, NR) \
  99. (__thread_self = (struct _pthread_descr_struct *)((void *) (DESCR) \
  100. + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE))
  101. /* Make sure we have the p_multiple_threads member in the thread structure.
  102. See below. */
  103. # define TLS_MULTIPLE_THREADS_IN_TCB 1
  104. /* Get the thread descriptor definition. */
  105. # include <linuxthreads/descr.h>
  106. /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
  107. different value to mean unset l_tls_offset. */
  108. # define NO_TLS_OFFSET -1
  109. # endif /* __ASSEMBLER__ */
  110. #elif !defined __ASSEMBLER__
  111. /* This overlaps the start of the pthread_descr. System calls
  112. and such use this to find the multiple_threads flag and need
  113. to use the same offset relative to the thread register in both
  114. single-threaded and multi-threaded code. */
  115. typedef struct
  116. {
  117. void *tcb; /* Never used. */
  118. dtv_t *dtv; /* Never used. */
  119. void *self; /* Used only if multithreaded, and rarely. */
  120. int multiple_threads; /* Only this member is really used. */
  121. } tcbhead_t;
  122. #define NONTLS_INIT_TP \
  123. do { \
  124. static const tcbhead_t nontls_init_tp = { .multiple_threads = 0 }; \
  125. __thread_self = (__typeof (__thread_self)) &nontls_init_tp; \
  126. } while (0)
  127. #endif /* HAVE_TLS_SUPPORT */
  128. #endif /* tls.h */