tls.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Definitions for thread-local data handling. linuxthreads/MIPS version.
  2. Copyright (C) 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 <stdbool.h>
  19. # include <pt-machine.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. # define READ_THREAD_POINTER() \
  32. ({ void *__result; \
  33. __asm__ __volatile__ (".set\tpush\n\t.set\tmips32r2\n\t" \
  34. "rdhwr\t%0, $29\n\t.set\tpop" : "=v" (__result)); \
  35. __result; })
  36. #else /* __ASSEMBLER__ */
  37. # include <tcb-offsets.h>
  38. /* Note: rd must be $v1 to be ABI-conformant. */
  39. # define READ_THREAD_POINTER(rd) \
  40. .set push; \
  41. .set mips32r2; \
  42. rdhwr rd, $29; \
  43. .set pop
  44. #endif /* __ASSEMBLER__ */
  45. /* LinuxThreads can only use TLS if both floating stacks (in the MIPS case,
  46. that means support for "rdhwr") and support from the tools are available.
  47. We have to define USE_TLS consistently, or ldsodefs.h will lay out types
  48. differently between an NPTL build and a LinuxThreads build. It can be set
  49. for libc.so and not libpthread.so, but only if we provide appropriate padding
  50. in the _pthread_descr_struct.
  51. Currently nothing defines FLOATING_STACKS. We could assume this based on
  52. kernel version once the TLS patches are available in kernel.org, but
  53. it hardly seems worth it. Use NPTL if you can.
  54. To avoid bothering with the TLS support code at all, use configure
  55. --without-tls. */
  56. #if defined HAVE_TLS_SUPPORT \
  57. && (defined FLOATING_STACKS || !defined IS_IN_libpthread)
  58. /* Signal that TLS support is available. */
  59. # define USE_TLS 1
  60. /* Include padding in _pthread_descr_struct so that libc can find p_errno,
  61. if libpthread will only include the padding because of the !IS_IN_libpthread
  62. check. */
  63. #ifndef FLOATING_STACKS
  64. # define INCLUDE_TLS_PADDING 1
  65. #endif
  66. # ifndef __ASSEMBLER__
  67. /* This layout is actually wholly private and not affected by the ABI.
  68. Nor does it overlap the pthread data structure, so we need nothing
  69. extra here at all. */
  70. typedef struct
  71. {
  72. dtv_t *dtv;
  73. void *private;
  74. } tcbhead_t;
  75. /* This is the size of the initial TCB. */
  76. # define TLS_INIT_TCB_SIZE 0
  77. /* Alignment requirements for the initial TCB. */
  78. # define TLS_INIT_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
  79. /* This is the size of the TCB. */
  80. # define TLS_TCB_SIZE 0
  81. /* Alignment requirements for the TCB. */
  82. # define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
  83. /* This is the size we need before TCB. */
  84. # define TLS_PRE_TCB_SIZE \
  85. (sizeof (struct _pthread_descr_struct) \
  86. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  87. /* The thread pointer (in hardware register $29) points to the end of
  88. the TCB + 0x7000, as for PowerPC. The pthread_descr structure is
  89. immediately in front of the TCB. */
  90. #define TLS_TCB_OFFSET 0x7000
  91. /* The DTV is allocated at the TP; the TCB is placed elsewhere. */
  92. /* This is not really true for powerpc64. We are following alpha
  93. where the DTV pointer is first doubleword in the TCB. */
  94. # define TLS_DTV_AT_TP 1
  95. /* Install the dtv pointer. The pointer passed is to the element with
  96. index -1 which contain the length. */
  97. # define INSTALL_DTV(TCBP, DTVP) \
  98. (((tcbhead_t *) (TCBP))[-1].dtv = (DTVP) + 1)
  99. /* Install new dtv for current thread. */
  100. # define INSTALL_NEW_DTV(DTV) (THREAD_DTV() = (DTV))
  101. /* Return dtv of given thread descriptor. */
  102. # define GET_DTV(TCBP) (((tcbhead_t *) (TCBP))[-1].dtv)
  103. /* Code to initially initialize the thread pointer. This might need
  104. special attention since 'errno' is not yet available and if the
  105. operation can cause a failure 'errno' must not be touched. */
  106. # define TLS_INIT_TP(tcbp, secondcall) \
  107. ({ INTERNAL_SYSCALL_DECL (err); \
  108. long result_var; \
  109. result_var = INTERNAL_SYSCALL (set_thread_area, err, 1, \
  110. (char *) (tcbp) + TLS_TCB_OFFSET); \
  111. INTERNAL_SYSCALL_ERROR_P (result_var, err) \
  112. ? "unknown error" : NULL; })
  113. /* Return the address of the dtv for the current thread. */
  114. # define THREAD_DTV() \
  115. (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
  116. /* Return the thread descriptor for the current thread. */
  117. # undef THREAD_SELF
  118. # define THREAD_SELF \
  119. ((pthread_descr) (READ_THREAD_POINTER () \
  120. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  121. /* Get the thread descriptor definition. */
  122. # include <linuxthreads/descr.h>
  123. /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some
  124. different value to mean unset l_tls_offset. */
  125. # define NO_TLS_OFFSET -1
  126. /* Initializing the thread pointer requires a syscall which may not be
  127. available, so don't do it if we don't need to. */
  128. # define TLS_INIT_TP_EXPENSIVE 1
  129. # endif /* __ASSEMBLER__ */
  130. #endif /* HAVE_TLS_SUPPORT */
  131. #endif /* tls.h */