tls.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Definition for thread-local data handling. NPTL/Nios II version.
  2. Copyright (C) 2012-2019 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 1
  17. #include <dl-sysdep.h>
  18. #ifndef __ASSEMBLER__
  19. # include <stdbool.h>
  20. # include <stddef.h>
  21. # include <stdint.h>
  22. # include <dl-dtv.h>
  23. #else /* __ASSEMBLER__ */
  24. # include <tcb-offsets.h>
  25. #endif /* __ASSEMBLER__ */
  26. #ifndef __ASSEMBLER__
  27. /* Get system call information. */
  28. # include <sysdep.h>
  29. /* The TP points to the start of the thread blocks. */
  30. # define TLS_DTV_AT_TP 1
  31. # define TLS_TCB_AT_TP 0
  32. /* Get the thread descriptor definition. */
  33. # include <nptl/descr.h>
  34. typedef struct
  35. {
  36. dtv_t *dtv;
  37. uintptr_t pointer_guard;
  38. unsigned spare[6];
  39. } tcbhead_t;
  40. register struct pthread *__thread_self __asm__("r23");
  41. #define READ_THREAD_POINTER() ((void *) __thread_self)
  42. /* This is the size of the initial TCB. Because our TCB is before the thread
  43. pointer, we don't need this. */
  44. # define TLS_INIT_TCB_SIZE 0
  45. /* Alignment requirements for the initial TCB. */
  46. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  47. /* This is the size of the TCB. Because our TCB is before the thread
  48. pointer, we don't need this. */
  49. # define TLS_TCB_SIZE 0
  50. /* Alignment requirements for the TCB. */
  51. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  52. /* This is the size we need before TCB - actually, it includes the TCB. */
  53. # define TLS_PRE_TCB_SIZE \
  54. (sizeof (struct pthread) \
  55. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  56. /* The thread pointer (in hardware register r23) points to the end of
  57. the TCB + 0x7000, as for PowerPC and MIPS. */
  58. # define TLS_TCB_OFFSET 0x7000
  59. /* Install the dtv pointer. The pointer passed is to the element with
  60. index -1 which contain the length. */
  61. # define INSTALL_DTV(tcbp, dtvp) \
  62. (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
  63. /* Install new dtv for current thread. */
  64. # define INSTALL_NEW_DTV(dtv) \
  65. (THREAD_DTV() = (dtv))
  66. /* Return dtv of given thread descriptor. */
  67. # define GET_DTV(tcbp) \
  68. (((tcbhead_t *) (tcbp))[-1].dtv)
  69. /* Code to initially initialize the thread pointer. */
  70. # define TLS_INIT_TP(tcbp) \
  71. (__thread_self = (struct pthread *) ((char *) tcbp + TLS_TCB_OFFSET), NULL)
  72. /* Value passed to 'clone' for initialization of the thread register. */
  73. # define TLS_DEFINE_INIT_TP(tp, pd) \
  74. void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
  75. /* Return the address of the dtv for the current thread. */
  76. # define THREAD_DTV() \
  77. (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
  78. /* Return the thread descriptor for the current thread. */
  79. # define THREAD_SELF \
  80. ((struct pthread *) (READ_THREAD_POINTER () \
  81. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  82. /* Magic for libthread_db to know how to do THREAD_SELF. */
  83. # define DB_THREAD_SELF \
  84. REGISTER (32, 32, 23 * 4, -TLS_PRE_TCB_SIZE - TLS_TCB_OFFSET)
  85. /* Access to data in the thread descriptor is easy. */
  86. # define THREAD_GETMEM(descr, member) \
  87. descr->member
  88. # define THREAD_GETMEM_NC(descr, member, idx) \
  89. descr->member[idx]
  90. # define THREAD_SETMEM(descr, member, value) \
  91. descr->member = (value)
  92. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  93. descr->member[idx] = (value)
  94. # define THREAD_GET_POINTER_GUARD() \
  95. (((tcbhead_t *) (READ_THREAD_POINTER () \
  96. - TLS_TCB_OFFSET))[-1].pointer_guard)
  97. # define THREAD_SET_POINTER_GUARD(value) \
  98. (THREAD_GET_POINTER_GUARD () = (value))
  99. # define THREAD_COPY_POINTER_GUARD(descr) \
  100. (((tcbhead_t *) ((void *) (descr) \
  101. + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
  102. = THREAD_GET_POINTER_GUARD())
  103. /* l_tls_offset == 0 is perfectly valid on Nios II, so we have to use some
  104. different value to mean unset l_tls_offset. */
  105. # define NO_TLS_OFFSET -1
  106. /* Get and set the global scope generation counter in struct pthread. */
  107. #define THREAD_GSCOPE_IN_TCB 1
  108. #define THREAD_GSCOPE_FLAG_UNUSED 0
  109. #define THREAD_GSCOPE_FLAG_USED 1
  110. #define THREAD_GSCOPE_FLAG_WAIT 2
  111. #define THREAD_GSCOPE_RESET_FLAG() \
  112. do \
  113. { int __res \
  114. = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
  115. THREAD_GSCOPE_FLAG_UNUSED); \
  116. if (__res == THREAD_GSCOPE_FLAG_WAIT) \
  117. lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
  118. } \
  119. while (0)
  120. #define THREAD_GSCOPE_SET_FLAG() \
  121. do \
  122. { \
  123. THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
  124. atomic_write_barrier (); \
  125. } \
  126. while (0)
  127. #define THREAD_GSCOPE_WAIT() \
  128. GL(dl_wait_lookup_done) ()
  129. #endif /* __ASSEMBLER__ */
  130. #endif /* tls.h */