tls.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Definition for thread-local data handling. NPTL/MIPS version.
  2. Copyright (C) 2005 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 1
  18. #include <dl-sysdep.h>
  19. #ifndef __ASSEMBLER__
  20. # include <stdbool.h>
  21. # include <stddef.h>
  22. # include <stdint.h>
  23. /* Type for the dtv. */
  24. typedef union dtv
  25. {
  26. size_t counter;
  27. struct
  28. {
  29. void *val;
  30. bool is_static;
  31. } pointer;
  32. } dtv_t;
  33. /* Note: rd must be $v1 to be ABI-conformant. */
  34. # define READ_THREAD_POINTER() \
  35. ({ void *__result; \
  36. asm volatile (".set\tpush\n\t.set\tmips32r2\n\t" \
  37. "rdhwr\t%0, $29\n\t.set\tpop" : "=v" (__result)); \
  38. __result; })
  39. #else /* __ASSEMBLER__ */
  40. # include <tcb-offsets.h>
  41. # define READ_THREAD_POINTER(rd) \
  42. .set push; \
  43. .set mips32r2; \
  44. rdhwr rd, $29; \
  45. .set pop
  46. #endif /* __ASSEMBLER__ */
  47. /* We require TLS support in the tools. */
  48. #ifndef HAVE_TLS_SUPPORT
  49. # error "TLS support is required."
  50. #endif
  51. /* Signal that TLS support is available. */
  52. #define USE_TLS 1
  53. #ifndef __ASSEMBLER__
  54. /* Get system call information. */
  55. # include <sysdep.h>
  56. /* The TP points to the start of the thread blocks. */
  57. # define TLS_DTV_AT_TP 1
  58. /* Get the thread descriptor definition. */
  59. # include <nptl/descr.h>
  60. typedef struct
  61. {
  62. dtv_t *dtv;
  63. void *private;
  64. } tcbhead_t;
  65. /* This is the size of the initial TCB. Because our TCB is before the thread
  66. pointer, we don't need this. */
  67. # define TLS_INIT_TCB_SIZE 0
  68. /* Alignment requirements for the initial TCB. */
  69. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  70. /* This is the size of the TCB. Because our TCB is before the thread
  71. pointer, we don't need this. */
  72. # define TLS_TCB_SIZE 0
  73. /* Alignment requirements for the TCB. */
  74. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  75. /* This is the size we need before TCB - actually, it includes the TCB. */
  76. # define TLS_PRE_TCB_SIZE \
  77. (sizeof (struct pthread) \
  78. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  79. /* The thread pointer (in hardware register $29) points to the end of
  80. the TCB + 0x7000, as for PowerPC. The pthread_descr structure is
  81. immediately in front of the TCB. */
  82. # define TLS_TCB_OFFSET 0x7000
  83. /* Install the dtv pointer. The pointer passed is to the element with
  84. index -1 which contain the length. */
  85. # define INSTALL_DTV(tcbp, dtvp) \
  86. (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
  87. /* Install new dtv for current thread. */
  88. # define INSTALL_NEW_DTV(dtv) \
  89. (THREAD_DTV() = (dtv))
  90. /* Return dtv of given thread descriptor. */
  91. # define GET_DTV(tcbp) \
  92. (((tcbhead_t *) (tcbp))[-1].dtv)
  93. /* Code to initially initialize the thread pointer. This might need
  94. special attention since 'errno' is not yet available and if the
  95. operation can cause a failure 'errno' must not be touched. */
  96. # define TLS_INIT_TP(tcbp, secondcall) \
  97. ({ INTERNAL_SYSCALL_DECL (err); \
  98. long result_var; \
  99. result_var = INTERNAL_SYSCALL (set_thread_area, err, 1, \
  100. (char *) (tcbp) + TLS_TCB_OFFSET); \
  101. INTERNAL_SYSCALL_ERROR_P (result_var, err) \
  102. ? "unknown error" : NULL; })
  103. /* Return the address of the dtv for the current thread. */
  104. # define THREAD_DTV() \
  105. (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
  106. /* Return the thread descriptor for the current thread. */
  107. # define THREAD_SELF \
  108. ((struct pthread *) (READ_THREAD_POINTER () \
  109. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  110. /* Magic for libthread_db to know how to do THREAD_SELF. */
  111. # define DB_THREAD_SELF \
  112. CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
  113. /* Access to data in the thread descriptor is easy. */
  114. # define THREAD_GETMEM(descr, member) \
  115. descr->member
  116. # define THREAD_GETMEM_NC(descr, member, idx) \
  117. descr->member[idx]
  118. # define THREAD_SETMEM(descr, member, value) \
  119. descr->member = (value)
  120. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  121. descr->member[idx] = (value)
  122. /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some
  123. different value to mean unset l_tls_offset. */
  124. # define NO_TLS_OFFSET -1
  125. #endif /* __ASSEMBLER__ */
  126. #endif /* tls.h */