tls.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Definition for thread-local data handling. NPTL/m68k version.
  2. Copyright (C) 2010-2017 Free Software Foundation, Inc.
  3. Contributed by Maxim Kuvyrkov <maxim@codesourcery.com>, 2010.
  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 1
  17. #ifndef __ASSEMBLER__
  18. # include <stdbool.h>
  19. # include <stddef.h>
  20. # include <stdint.h>
  21. /* Type for the dtv. */
  22. typedef union dtv
  23. {
  24. size_t counter;
  25. struct
  26. {
  27. void *val;
  28. bool is_static;
  29. } pointer;
  30. } dtv_t;
  31. /* Value used for dtv entries for which the allocation is delayed. */
  32. #define TLS_DTV_UNALLOCATED ((void *) -1l)
  33. #else /* __ASSEMBLER__ */
  34. # include <tcb-offsets.h>
  35. #endif /* __ASSEMBLER__ */
  36. /* We require TLS support in the tools. */
  37. #define HAVE_TLS_SUPPORT 1
  38. #define HAVE_TLS_MODEL_ATTRIBUTE 1
  39. #define HAVE___THREAD 1
  40. /* Signal that TLS support is available. */
  41. #define USE_TLS 1
  42. #ifndef __ASSEMBLER__
  43. /* Get system call information. */
  44. # include <sysdep.h>
  45. /* The TP points to the start of the thread blocks. */
  46. # define TLS_DTV_AT_TP 1
  47. /* Get the thread descriptor definition. */
  48. # include <../../descr.h>
  49. typedef struct
  50. {
  51. dtv_t *dtv;
  52. void *private;
  53. } tcbhead_t;
  54. /* This is the size of the initial TCB. Because our TCB is before the thread
  55. pointer, we don't need this. */
  56. # define TLS_INIT_TCB_SIZE 0
  57. /* Alignment requirements for the initial TCB. */
  58. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  59. /* This is the size of the TCB. Because our TCB is before the thread
  60. pointer, we don't need this. */
  61. # define TLS_TCB_SIZE 0
  62. /* Alignment requirements for the TCB. */
  63. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  64. /* This is the size we need before TCB - actually, it includes the TCB. */
  65. # define TLS_PRE_TCB_SIZE \
  66. (sizeof (struct pthread) \
  67. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  68. /* The thread pointer (TP) points to the end of the
  69. TCB + 0x7000, as for PowerPC and MIPS. This implies that TCB address is
  70. TP - 0x7000. As we define TLS_DTV_AT_TP we can
  71. assume that the pthread struct is allocated immediately ahead of the
  72. TCB. This implies that the pthread_descr address is
  73. TP - (TLS_PRE_TCB_SIZE + 0x7000). */
  74. # define TLS_TCB_OFFSET 0x7000
  75. /* Install the dtv pointer. The pointer passed is to the element with
  76. index -1 which contain the length. */
  77. # define INSTALL_DTV(tcbp, dtvp) \
  78. ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
  79. /* Install new dtv for current thread. */
  80. # define INSTALL_NEW_DTV(dtv) \
  81. (THREAD_DTV () = (dtv))
  82. /* Return dtv of given thread descriptor. */
  83. # define GET_DTV(tcbp) \
  84. (((tcbhead_t *) (tcbp))[-1].dtv)
  85. /* Code to initially initialize the thread pointer. This might need
  86. special attention since 'errno' is not yet available and if the
  87. operation can cause a failure 'errno' must not be touched. */
  88. # define TLS_INIT_TP(tcbp, secondcall) \
  89. ({ \
  90. INTERNAL_SYSCALL_DECL (err); \
  91. int result_var; \
  92. result_var = INTERNAL_SYSCALL (set_thread_area, err, 1, \
  93. ((void *) (tcbp)) + TLS_TCB_OFFSET); \
  94. INTERNAL_SYSCALL_ERROR_P (result_var, err) \
  95. ? "unknown error" : NULL; })
  96. # define TLS_DEFINE_INIT_TP(tp, pd) \
  97. void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
  98. extern void * __m68k_read_tp (void);
  99. /* Return the address of the dtv for the current thread. */
  100. # define THREAD_DTV() \
  101. (((tcbhead_t *) (__m68k_read_tp () - TLS_TCB_OFFSET))[-1].dtv)
  102. /* Return the thread descriptor for the current thread. */
  103. # define THREAD_SELF \
  104. ((struct pthread *) (__m68k_read_tp () - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  105. /* Magic for libthread_db to know how to do THREAD_SELF. */
  106. # define DB_THREAD_SELF \
  107. CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
  108. /* Access to data in the thread descriptor is easy. */
  109. # define THREAD_GETMEM(descr, member) \
  110. descr->member
  111. # define THREAD_GETMEM_NC(descr, member, idx) \
  112. descr->member[idx]
  113. # define THREAD_SETMEM(descr, member, value) \
  114. descr->member = (value)
  115. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  116. descr->member[idx] = (value)
  117. /* l_tls_offset == 0 is perfectly valid on M68K, so we have to use some
  118. different value to mean unset l_tls_offset. */
  119. # define NO_TLS_OFFSET -1
  120. /* Get and set the global scope generation counter in struct pthread. */
  121. #define THREAD_GSCOPE_FLAG_UNUSED 0
  122. #define THREAD_GSCOPE_FLAG_USED 1
  123. #define THREAD_GSCOPE_FLAG_WAIT 2
  124. #define THREAD_GSCOPE_RESET_FLAG() \
  125. do \
  126. { int __res \
  127. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  128. THREAD_GSCOPE_FLAG_UNUSED); \
  129. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  130. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  131. } \
  132. while (0)
  133. #define THREAD_GSCOPE_SET_FLAG() \
  134. do \
  135. { \
  136. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  137. atomic_write_barrier (); \
  138. } \
  139. while (0)
  140. #define THREAD_GSCOPE_WAIT() \
  141. GL(dl_wait_lookup_done) ()
  142. #endif /* __ASSEMBLER__ */
  143. #endif /* tls.h */