tls.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Definition for thread-local data handling. linuxthreads/i386 version.
  2. Copyright (C) 2002, 2003, 2004, 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. # include <pt-machine.h>
  18. #ifndef __ASSEMBLER__
  19. # include <stdbool.h>
  20. # include <stddef.h>
  21. # include <stdint.h>
  22. /* Type for the dtv. */
  23. typedef union dtv
  24. {
  25. size_t counter;
  26. void *pointer;
  27. } dtv_t;
  28. typedef struct
  29. {
  30. void *tcb; /* Pointer to the TCB. Not necessary the
  31. thread descriptor used by libpthread. */
  32. dtv_t *dtv;
  33. void *self; /* Pointer to the thread descriptor. */
  34. } tcbhead_t;
  35. #endif
  36. /* We can support TLS only if the floating-stack support is available. */
  37. #if defined FLOATING_STACKS && defined HAVE_TLS_SUPPORT
  38. /* Signal that TLS support is available. */
  39. //# define USE_TLS 1
  40. # ifndef __ASSEMBLER__
  41. /* Get system call information. */
  42. # include <sysdep.h>
  43. /* Get the thread descriptor definition. */
  44. # include <linuxthreads/descr.h>
  45. /* This is the size of the initial TCB. */
  46. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  47. /* Alignment requirements for the initial TCB. */
  48. # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
  49. /* This is the size of the TCB. */
  50. # define TLS_TCB_SIZE sizeof (struct _pthread_descr_struct)
  51. /* Alignment requirements for the TCB. */
  52. # define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
  53. /* The TCB can have any size and the memory following the address the
  54. thread pointer points to is unspecified. Allocate the TCB there. */
  55. # define TLS_TCB_AT_TP 1
  56. /* Install the dtv pointer. The pointer passed is to the element with
  57. index -1 which contain the length. */
  58. # define INSTALL_DTV(descr, dtvp) \
  59. ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
  60. /* Install new dtv for current thread. */
  61. # define INSTALL_NEW_DTV(dtv) \
  62. ({ struct _pthread_descr_struct *__descr; \
  63. THREAD_SETMEM (__descr, p_header.data.dtvp, (dtv)); })
  64. /* Return dtv of given thread descriptor. */
  65. # define GET_DTV(descr) \
  66. (((tcbhead_t *) (descr))->dtv)
  67. # ifdef __PIC__
  68. # define TLS_EBX_ARG "r"
  69. # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
  70. # else
  71. # define TLS_EBX_ARG "b"
  72. # define TLS_LOAD_EBX
  73. # endif
  74. # define TLS_DO_MODIFY_LDT(descr, nr) \
  75. ({ \
  76. struct modify_ldt_ldt_s ldt_entry = \
  77. { nr, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
  78. 1, 0, 0, 1, 0, 1, 0 }; \
  79. int result; \
  80. __asm__ __volatile__ (TLS_LOAD_EBX \
  81. "int $0x80\n\t" \
  82. TLS_LOAD_EBX \
  83. : "=a" (result) \
  84. : "0" (__NR_modify_ldt), \
  85. /* The extra argument with the "m" constraint is necessary \
  86. to let the compiler know that we are accessing LDT_ENTRY \
  87. here. */ \
  88. "m" (ldt_entry), TLS_EBX_ARG (1), "c" (&ldt_entry), \
  89. "d" (sizeof (ldt_entry))); \
  90. __builtin_expect (result, 0) != 0 ? -1 : nr * 8 + 7; \
  91. })
  92. # define TLS_DO_SET_THREAD_AREA(descr, secondcall) \
  93. ({ \
  94. struct modify_ldt_ldt_s ldt_entry = \
  95. { -1, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
  96. 1, 0, 0, 1, 0, 1, 0 }; \
  97. int result; \
  98. if (secondcall) \
  99. ldt_entry.entry_number = ({ int _gs; \
  100. __asm__ ("movw %%gs, %w0" : "=q" (_gs)); \
  101. (_gs & 0xffff) >> 3; }); \
  102. __asm__ __volatile__ (TLS_LOAD_EBX \
  103. "int $0x80\n\t" \
  104. TLS_LOAD_EBX \
  105. : "=a" (result), "=m" (ldt_entry.entry_number) \
  106. : "0" (__NR_set_thread_area), \
  107. /* The extra argument with the "m" constraint is necessary \
  108. to let the compiler know that we are accessing LDT_ENTRY \
  109. here. */ \
  110. TLS_EBX_ARG (&ldt_entry), "m" (ldt_entry)); \
  111. __builtin_expect (result, 0) == 0 ? ldt_entry.entry_number * 8 + 3 : -1; \
  112. })
  113. # ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
  114. # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
  115. TLS_DO_SET_THREAD_AREA (descr, firstcall)
  116. # elif defined __NR_set_thread_area
  117. # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
  118. ({ int __seg = TLS_DO_SET_THREAD_AREA (descr, secondcall); \
  119. __seg == -1 ? TLS_DO_MODIFY_LDT (descr, 0) : __seg; })
  120. # else
  121. # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
  122. TLS_DO_MODIFY_LDT ((descr), 0)
  123. # endif
  124. /* Code to initially initialize the thread pointer. This might need
  125. special attention since 'errno' is not yet available and if the
  126. operation can cause a failure 'errno' must not be touched. */
  127. # define TLS_INIT_TP(descr, secondcall) \
  128. ({ \
  129. void *_descr = (descr); \
  130. tcbhead_t *head = _descr; \
  131. int __gs; \
  132. \
  133. head->tcb = _descr; \
  134. /* For now the thread descriptor is at the same address. */ \
  135. head->self = _descr; \
  136. \
  137. __gs = TLS_SETUP_GS_SEGMENT (_descr, secondcall); \
  138. if (__builtin_expect (__gs, 7) != -1) \
  139. { \
  140. __asm__ ("movw %w0, %%gs" : : "q" (__gs)); \
  141. __gs = 0; \
  142. } \
  143. __gs; \
  144. })
  145. /* Return the address of the dtv for the current thread. */
  146. # define THREAD_DTV() \
  147. ({ struct _pthread_descr_struct *__descr; \
  148. THREAD_GETMEM (__descr, p_header.data.dtvp); })
  149. # endif /* FLOATING_STACKS && HAVE_TLS_SUPPORT */
  150. #endif /* __ASSEMBLER__ */
  151. #endif /* tls.h */