tls.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Definitions for thread-local data handling. linuxthreads/ARM version.
  2. Copyright (C) 2004 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. typedef struct
  33. {
  34. dtv_t *dtv;
  35. /* Reserved for the thread implementation. Unused in LinuxThreads. */
  36. void *private;
  37. } tcbhead_t;
  38. #endif
  39. /* We can support TLS only if the floating-stack support is available.
  40. However, we want to compile in the support and test at runtime whether
  41. the running kernel can support it or not. To avoid bothering with the
  42. TLS support code at all, use configure --without-tls.
  43. We need USE_TLS to be consistently defined, for ldsodefs.h conditionals.
  44. But some of the code below can cause problems in building libpthread
  45. (e.g. useldt.h will defined FLOATING_STACKS when it shouldn't). */
  46. /* LinuxThreads can only support TLS if both floating stacks and support
  47. 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.
  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. /* Get system call information. */
  68. # include <sysdep.h>
  69. /* This is the size of the initial TCB. */
  70. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  71. /* Alignment requirements for the initial TCB. */
  72. # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
  73. /* This is the size of the TCB. */
  74. # define TLS_TCB_SIZE sizeof (tcbhead_t)
  75. /* Alignment requirements for the TCB. */
  76. # define TLS_TCB_ALIGN __alignof__ (tcbhead_t)
  77. /* This is the size we need before TCB. */
  78. # define TLS_PRE_TCB_SIZE sizeof (struct _pthread_descr_struct)
  79. /* The DTV is allocated at the TP; the TCB is placed elsewhere. */
  80. # define TLS_DTV_AT_TP 1
  81. /* Install the dtv pointer. The pointer passed is to the element with
  82. index -1 which contain the length. */
  83. # define INSTALL_DTV(TCBP, DTVP) \
  84. (((tcbhead_t *) (TCBP))->dtv = (DTVP) + 1)
  85. /* Install new dtv for current thread. */
  86. # define INSTALL_NEW_DTV(DTV) \
  87. (((tcbhead_t *)__builtin_thread_pointer ())->dtv = (DTV))
  88. /* Return dtv of given thread descriptor. */
  89. # define GET_DTV(TCBP) \
  90. (((tcbhead_t *) (TCBP))->dtv)
  91. /* Code to initially initialize the thread pointer. This might need
  92. special attention since 'errno' is not yet available and if the
  93. operation can cause a failure 'errno' must not be touched. */
  94. # define TLS_INIT_TP(TCBP, SECONDCALL) \
  95. ({ INTERNAL_SYSCALL_DECL (err); \
  96. long result_var; \
  97. result_var = INTERNAL_SYSCALL_ARM (set_tls, err, 1, (TCBP)); \
  98. INTERNAL_SYSCALL_ERROR_P (result_var, err) \
  99. ? "unknown error" : NULL; })
  100. /* Return the address of the dtv for the current thread. */
  101. # define THREAD_DTV() \
  102. (((tcbhead_t *)__builtin_thread_pointer ())->dtv)
  103. /* Return the thread descriptor for the current thread. */
  104. # undef THREAD_SELF
  105. # define THREAD_SELF \
  106. ((pthread_descr)__builtin_thread_pointer () - 1)
  107. # undef INIT_THREAD_SELF
  108. # define INIT_THREAD_SELF(DESCR, NR) \
  109. TLS_INIT_TP ((struct _pthread_descr_struct *)(DESCR) + 1, 0)
  110. /* Get the thread descriptor definition. */
  111. # include <linuxthreads/descr.h>
  112. /* ??? Generic bits of LinuxThreads may call these macros with
  113. DESCR set to NULL. We are expected to be able to reference
  114. the "current" value.
  115. In our case, we'd really prefer to use DESCR, since lots of
  116. PAL_code calls would be expensive. We can only trust that
  117. the compiler does its job and unifies the multiple
  118. __builtin_thread_pointer instances. */
  119. #define THREAD_GETMEM(descr, member) \
  120. ((void) sizeof (descr), THREAD_SELF->member)
  121. #define THREAD_GETMEM_NC(descr, member) \
  122. ((void) sizeof (descr), THREAD_SELF->member)
  123. #define THREAD_SETMEM(descr, member, value) \
  124. ((void) sizeof (descr), THREAD_SELF->member = (value))
  125. #define THREAD_SETMEM_NC(descr, member, value) \
  126. ((void) sizeof (descr), THREAD_SELF->member = (value))
  127. /* Initializing the thread pointer will generate a SIGILL if the syscall
  128. is not available. */
  129. #define TLS_INIT_TP_EXPENSIVE 1
  130. # endif /* HAVE_TLS_SUPPORT */
  131. #endif /* __ASSEMBLER__ */
  132. #endif /* tls.h */