tls.h 5.6 KB

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