tls.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #ifndef _TLS_H
  2. #define _TLS_H 1
  3. #ifndef __ASSEMBLER__
  4. # include <stdbool.h>
  5. # include <stddef.h>
  6. # include <stdint.h>
  7. /* Type for the dtv. */
  8. typedef union dtv
  9. {
  10. size_t counter;
  11. struct
  12. {
  13. void *val;
  14. bool is_static;
  15. } pointer;
  16. } dtv_t;
  17. #ifdef __CSKYABIV2__
  18. /* define r31 as thread pointer register? */
  19. # define READ_THREAD_POINTER() \
  20. ({ void *__result; \
  21. __asm__ __volatile__ ("mov %0, r31" \
  22. : "=r" (__result)); \
  23. __result; })
  24. #else
  25. # define READ_THREAD_POINTER() \
  26. ({ register unsigned int __result __asm__("a0"); \
  27. __asm__ __volatile__ ("trap 3;" \
  28. : "=r" (__result) : : ); \
  29. __result; })
  30. #endif
  31. #else /* __ASSEMBLER__ */
  32. # include <tcb-offsets.h>
  33. # ifdef __CSKYABIV2__
  34. /* define r31 as thread pointer register? */
  35. # define READ_THREAD_POINTER() \
  36. mov r0, r31;
  37. # else
  38. # define READ_THREAD_POINTER() \
  39. trap 3;
  40. # endif
  41. #endif /* __ASSEMBLER__ */
  42. /* We require TLS support in the tools. */
  43. #define HAVE_TLS_SUPPORT 1
  44. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  45. #define HAVE___THREAD 1
  46. /* Signal that TLS support is available. */
  47. #define USE_TLS 1
  48. # ifndef __ASSEMBLER__
  49. /* Get system call information. */
  50. # include <sysdep.h>
  51. /* The TP points to the start of the thread blocks. */
  52. # define TLS_DTV_AT_TP 1
  53. /* Get the thread descriptor definition. */
  54. # include <../../descr.h>
  55. typedef struct
  56. {
  57. dtv_t *dtv;
  58. void *private;
  59. } tcbhead_t;
  60. /* This is the size of the initial TCB. */
  61. # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
  62. /* Alignment requirements for the initial TCB. */
  63. # define TLS_INIT_TCB_ALIGN 16
  64. /* This is the size of the TCB. */
  65. # define TLS_TCB_SIZE sizeof (tcbhead_t)
  66. /* Alignment requirements for the TCB. */
  67. # define TLS_TCB_ALIGN 16
  68. /* This is the size we need before TCB. */
  69. # define TLS_PRE_TCB_SIZE sizeof (struct pthread)
  70. /* The thread pointer (in hardware register $29) points to the end of
  71. the TCB + 0x7000, as for PowerPC. The pthread_descr structure is
  72. immediately in front of the TCB. */
  73. # define TLS_TCB_OFFSET 0//0x7000
  74. /* Install the dtv pointer. The pointer passed is to the element with
  75. index -1 which contain the length. */
  76. # define INSTALL_DTV(tcbp, dtvp) \
  77. (((tcbhead_t *) (tcbp))->dtv = (dtvp) + 1)
  78. /* Install new dtv for current thread. */
  79. # define INSTALL_NEW_DTV(dtv) \
  80. (THREAD_DTV() = (dtv))
  81. /* Return dtv of given thread descriptor. */
  82. # define GET_DTV(tcbp) \
  83. (((tcbhead_t *) (tcbp))->dtv)
  84. /* Code to initially initialize the thread pointer. This might need
  85. special attention since 'errno' is not yet available and if the
  86. operation can cause a failure 'errno' must not be touched. */
  87. # define TLS_INIT_TP(tcbp, secondcall) \
  88. ({ INTERNAL_SYSCALL_DECL (err); \
  89. long result_var; \
  90. result_var = INTERNAL_SYSCALL (set_thread_area, err, 1, \
  91. (char *) (tcbp) + TLS_TCB_OFFSET); \
  92. INTERNAL_SYSCALL_ERROR_P (result_var, err) \
  93. ? "unknown error" : NULL; })
  94. /* Return the address of the dtv for the current thread. */
  95. # define THREAD_DTV() \
  96. (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))->dtv)
  97. /* Return the thread descriptor for the current thread. */
  98. # undef THREAD_SELF
  99. # define THREAD_SELF \
  100. ((struct pthread *) (READ_THREAD_POINTER () \
  101. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  102. /* Magic for libthread_db to know how to do THREAD_SELF. */
  103. # define DB_THREAD_SELF \
  104. CONST_THREAD_AREA (32, sizeof (struct pthread))
  105. /* Access to data in the thread descriptor is easy. */
  106. #define THREAD_GETMEM(descr, member) \
  107. descr->member
  108. #define THREAD_GETMEM_NC(descr, member, idx) \
  109. descr->member[idx]
  110. #define THREAD_SETMEM(descr, member, value) \
  111. descr->member = (value)
  112. #define THREAD_SETMEM_NC(descr, member, idx, value) \
  113. descr->member[idx] = (value)
  114. /* Initializing the thread pointer will generate a SIGILL if the syscall
  115. is not available. */
  116. #define TLS_INIT_TP_EXPENSIVE 1
  117. /* Get and set the global scope generation counter in struct pthread. */
  118. #define THREAD_GSCOPE_FLAG_UNUSED 0
  119. #define THREAD_GSCOPE_FLAG_USED 1
  120. #define THREAD_GSCOPE_FLAG_WAIT 2
  121. #define THREAD_GSCOPE_RESET_FLAG() \
  122. do \
  123. { int __res \
  124. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  125. THREAD_GSCOPE_FLAG_UNUSED); \
  126. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  127. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  128. } \
  129. while (0)
  130. #define THREAD_GSCOPE_SET_FLAG() \
  131. do \
  132. { \
  133. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  134. atomic_write_barrier (); \
  135. } \
  136. while (0)
  137. #define THREAD_GSCOPE_WAIT() \
  138. GL(dl_wait_lookup_done) ()
  139. #endif /* __ASSEMBLER__ */
  140. #endif /* tls.h */