tls.h 5.4 KB

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