tls.h 5.1 KB

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