tls.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. # 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. #else /* __ASSEMBLER__ */
  34. # include <tcb-offsets.h>
  35. #endif /* __ASSEMBLER__ */
  36. /* We require TLS support in the tools. */
  37. #ifndef HAVE_TLS_SUPPORT
  38. # error "TLS support is required."
  39. #endif
  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. /* We use the multiple_threads field in the pthread struct */
  48. #define TLS_MULTIPLE_THREADS_IN_TCB 1
  49. /* Get the thread descriptor definition. */
  50. # include <nptl/descr.h>
  51. /* This layout is actually wholly private and not affected by the ABI.
  52. Nor does it overlap the pthread data structure, so we need nothing
  53. extra here at all. */
  54. typedef struct
  55. {
  56. dtv_t *dtv;
  57. } tcbhead_t;
  58. /* This is the size of the initial TCB. */
  59. # define TLS_INIT_TCB_SIZE 0
  60. /* Alignment requirements for the initial TCB. */
  61. # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
  62. /* This is the size of the TCB. */
  63. # define TLS_TCB_SIZE 0
  64. /* Alignment requirements for the TCB. */
  65. # define TLS_TCB_ALIGN __alignof__ (struct pthread)
  66. /* This is the size we need before TCB. */
  67. # define TLS_PRE_TCB_SIZE \
  68. (sizeof (struct pthread) \
  69. + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
  70. # ifndef __powerpc64__
  71. /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
  72. register void *__thread_register __asm__ ("r2");
  73. # define PT_THREAD_POINTER PT_R2
  74. # else
  75. /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
  76. register void *__thread_register __asm__ ("r13");
  77. # define PT_THREAD_POINTER PT_R13
  78. # endif
  79. /* The following assumes that TP (R2 or R13) points to the end of the
  80. TCB + 0x7000 (per the ABI). This implies that TCB address is
  81. TP - 0x7000. As we define TLS_DTV_AT_TP we can
  82. assume that the pthread struct is allocated immediately ahead of the
  83. TCB. This implies that the pthread_descr address is
  84. TP - (TLS_PRE_TCB_SIZE + 0x7000). */
  85. # define TLS_TCB_OFFSET 0x7000
  86. /* Install the dtv pointer. The pointer passed is to the element with
  87. index -1 which contain the length. */
  88. # define INSTALL_DTV(tcbp, dtvp) \
  89. ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
  90. /* Install new dtv for current thread. */
  91. # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
  92. /* Return dtv of given thread descriptor. */
  93. # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
  94. /* Code to initially initialize the thread pointer. This might need
  95. special attention since 'errno' is not yet available and if the
  96. operation can cause a failure 'errno' must not be touched. */
  97. # define TLS_INIT_TP(tcbp, secondcall) \
  98. (__thread_register = (void *) (tcbp) + TLS_TCB_OFFSET, NULL)
  99. /* Return the address of the dtv for the current thread. */
  100. # define THREAD_DTV() \
  101. (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
  102. /* Return the thread descriptor for the current thread. */
  103. # define THREAD_SELF \
  104. ((struct pthread *) (__thread_register \
  105. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
  106. /* Magic for libthread_db to know how to do THREAD_SELF. */
  107. # define DB_THREAD_SELF \
  108. REGISTER (32, 32, PT_THREAD_POINTER * 4, \
  109. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \
  110. REGISTER (64, 64, PT_THREAD_POINTER * 8, \
  111. - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
  112. /* Read member of the thread descriptor directly. */
  113. # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
  114. /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
  115. # define THREAD_GETMEM_NC(descr, member, idx) \
  116. ((void)(descr), (THREAD_SELF)->member[idx])
  117. /* Set member of the thread descriptor directly. */
  118. # define THREAD_SETMEM(descr, member, value) \
  119. ((void)(descr), (THREAD_SELF)->member = (value))
  120. /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
  121. # define THREAD_SETMEM_NC(descr, member, idx, value) \
  122. ((void)(descr), (THREAD_SELF)->member[idx] = (value))
  123. /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
  124. different value to mean unset l_tls_offset. */
  125. # define NO_TLS_OFFSET -1
  126. #endif /* __ASSEMBLER__ */
  127. #endif /* tls.h */