tls.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Definition for thread-local data handling. NPTL/PowerPC version.
  2. Copyright (C) 2003, 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. #ifndef __ASSEMBLER__
  19. # include <stdbool.h>
  20. # include <stddef.h>
  21. # include <stdint.h>
  22. /* Type for the dtv. */
  23. typedef union dtv
  24. {
  25. size_t counter;
  26. struct
  27. {
  28. void *val;
  29. bool is_static;
  30. } pointer;
  31. } dtv_t;
  32. #else /* __ASSEMBLER__ */
  33. # include <tcb-offsets.h>
  34. #endif /* __ASSEMBLER__ */
  35. /* We require TLS support in the tools. */
  36. #ifndef HAVE_TLS_SUPPORT
  37. # error "TLS support is required."
  38. #endif
  39. /* Signal that TLS support is available. */
  40. # define USE_TLS 1
  41. #ifndef __ASSEMBLER__
  42. /* Get system call information. */
  43. # include <sysdep.h>
  44. /* The TP points to the start of the thread blocks. */
  45. # define TLS_DTV_AT_TP 1
  46. /* We use the multiple_threads field in the pthread struct */
  47. #define TLS_MULTIPLE_THREADS_IN_TCB 1
  48. /* Get the thread descriptor definition. */
  49. # include <nptl/descr.h>
  50. /* The stack_guard is accessed directly by GCC -fstack-protector code,
  51. so it is a part of public ABI. The dtv field is private. */
  52. typedef struct
  53. {
  54. uintptr_t stack_guard;
  55. dtv_t *dtv;
  56. } tcbhead_t;
  57. /* This is the size of the initial TCB. */
  58. # define TLS_INIT_TCB_SIZE 0
  59. /* Alignment requirements for the initial TCB. */
  60. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  61. /* This is the size of the TCB. */
  62. # define TLS_TCB_SIZE 0
  63. /* Alignment requirements for the TCB. */
  64. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  65. /* This is the size we need before TCB. */
  66. # define TLS_PRE_TCB_SIZE \
  67. (sizeof (struct pthread) \
  68. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  69. # ifndef __powerpc64__
  70. /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
  71. register void *__thread_register __asm__ ("r2");
  72. # define PT_THREAD_POINTER PT_R2
  73. # else
  74. /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
  75. register void *__thread_register __asm__ ("r13");
  76. # define PT_THREAD_POINTER PT_R13
  77. # endif
  78. /* The following assumes that TP (R2 or R13) points to the end of the
  79. TCB + 0x7000 (per the ABI). This implies that TCB address is
  80. TP - 0x7000. As we define TLS_DTV_AT_TP we can
  81. assume that the pthread struct is allocated immediately ahead of the
  82. TCB. This implies that the pthread_descr address is
  83. TP - (TLS_PRE_TCB_SIZE + 0x7000). */
  84. # define TLS_TCB_OFFSET 0x7000
  85. /* Install the dtv pointer. The pointer passed is to the element with
  86. index -1 which contain the length. */
  87. # define INSTALL_DTV(tcbp, dtvp) \
  88. ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
  89. /* Install new dtv for current thread. */
  90. # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
  91. /* Return dtv of given thread descriptor. */
  92. # define GET_DTV(tcbp) (((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. (__thread_register = (void *) (tcbp) + TLS_TCB_OFFSET, NULL)
  98. /* Return the address of the dtv for the current thread. */
  99. # define THREAD_DTV() \
  100. (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
  101. /* Return the thread descriptor for the current thread. */
  102. # define THREAD_SELF \
  103. ((struct pthread *) (__thread_register \
  104. - 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. REGISTER (32, 32, PT_THREAD_POINTER * 4, \
  108. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \
  109. REGISTER (64, 64, PT_THREAD_POINTER * 8, \
  110. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
  111. /* Read member of the thread descriptor directly. */
  112. # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
  113. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  114. # define THREAD_GETMEM_NC(descr, member, idx) \
  115. ((void)(descr), (THREAD_SELF)->member[idx])
  116. /* Set member of the thread descriptor directly. */
  117. # define THREAD_SETMEM(descr, member, value) \
  118. ((void)(descr), (THREAD_SELF)->member = (value))
  119. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  120. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  121. ((void)(descr), (THREAD_SELF)->member[idx] = (value))
  122. /* Set the stack guard field in TCB head. */
  123. # define THREAD_SET_STACK_GUARD(value) \
  124. (((tcbhead_t *) ((char *) __thread_register \
  125. - TLS_TCB_OFFSET))[-1].stack_guard = (value))
  126. # define THREAD_COPY_STACK_GUARD(descr) \
  127. (((tcbhead_t *) ((char *) (descr) \
  128. + TLS_PRE_TCB_SIZE))[-1].stack_guard \
  129. = ((tcbhead_t *) ((char *) __thread_register \
  130. - TLS_TCB_OFFSET))[-1].stack_guard)
  131. /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
  132. different value to mean unset l_tls_offset. */
  133. # define NO_TLS_OFFSET -1
  134. #endif /* __ASSEMBLER__ */
  135. #endif /* tls.h */