tls.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Definitions for thread-local data handling. linuxthreads/x86-64 version.
  2. Copyright (C) 2002 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 <pt-machine.h>
  20. # include <stddef.h>
  21. /* Type for the dtv. */
  22. typedef union dtv
  23. {
  24. size_t counter;
  25. void *pointer;
  26. } dtv_t;
  27. typedef struct
  28. {
  29. void *tcb; /* Pointer to the TCB. Not necessary the
  30. thread descriptor used by libpthread. */
  31. dtv_t *dtv;
  32. void *self; /* Pointer to the thread descriptor. */
  33. int multiple_threads;
  34. } tcbhead_t;
  35. #else /* __ASSEMBLER__ */
  36. # include <tcb-offsets.h>
  37. #endif
  38. #ifdef HAVE_TLS_SUPPORT
  39. /* Signal that TLS support is available. */
  40. # define USE_TLS 1
  41. # ifndef __ASSEMBLER__
  42. /* Get system call information. */
  43. # include <sysdep.h>
  44. /* Get the thread descriptor definition. */
  45. # include <linuxthreads/descr.h>
  46. /* This is the size of the initial TCB. */
  47. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  48. /* Alignment requirements for the initial TCB. */
  49. # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
  50. /* This is the size of the TCB. */
  51. # define TLS_TCB_SIZE sizeof (struct _pthread_descr_struct)
  52. /* Alignment requirements for the TCB. */
  53. # define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
  54. /* The TCB can have any size and the memory following the address the
  55. thread pointer points to is unspecified. Allocate the TCB there. */
  56. # define TLS_TCB_AT_TP 1
  57. /* Install the dtv pointer. The pointer passed is to the element with
  58. index -1 which contain the length. */
  59. # define INSTALL_DTV(descr, dtvp) \
  60. ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
  61. /* Install new dtv for current thread. */
  62. # define INSTALL_NEW_DTV(dtv) \
  63. ({ struct _pthread_descr_struct *__descr; \
  64. THREAD_SETMEM (__descr, p_header.data.dtvp, (dtv)); })
  65. /* Return dtv of given thread descriptor. */
  66. # define GET_DTV(descr) \
  67. (((tcbhead_t *) (descr))->dtv)
  68. /* Code to initially initialize the thread pointer. This might need
  69. special attention since 'errno' is not yet available and if the
  70. operation can cause a failure 'errno' must not be touched. */
  71. # define TLS_INIT_TP(descr, secondcall) \
  72. ({ \
  73. void *_descr = (descr); \
  74. tcbhead_t *head = _descr; \
  75. long int _result; \
  76. \
  77. head->tcb = _descr; \
  78. /* For now the thread descriptor is at the same address. */ \
  79. head->self = _descr; \
  80. \
  81. asm volatile ("syscall" \
  82. : "=a" (_result) \
  83. : "0" ((unsigned long int) __NR_arch_prctl), \
  84. "D" ((unsigned long int) ARCH_SET_FS), \
  85. "S" (_descr) \
  86. : "memory", "cc", "r11", "cx"); \
  87. \
  88. _result ? "cannot set %fs base address for thread-local storage" : 0; \
  89. })
  90. /* Indicate that dynamic linker shouldn't try to initialize TLS even
  91. when no PT_TLS segments are found in the program and libraries
  92. it is linked against. */
  93. # define TLS_INIT_TP_EXPENSIVE 1
  94. /* Return the address of the dtv for the current thread. */
  95. # define THREAD_DTV() \
  96. ({ struct _pthread_descr_struct *__descr; \
  97. THREAD_GETMEM (__descr, p_header.data.dtvp); })
  98. # endif /* HAVE_TLS_SUPPORT */
  99. #endif /* __ASSEMBLER__ */
  100. #endif /* tls.h */