1 /* Definition for thread-local data handling. NPTL/PowerPC version.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
22 # include <dl-sysdep.h>
29 /* Type for the dtv. */
40 #else /* __ASSEMBLER__ */
41 # include <tcb-offsets.h>
42 #endif /* __ASSEMBLER__ */
47 /* Get system call information. */
50 /* The TP points to the start of the thread blocks. */
51 # define TLS_DTV_AT_TP 1
52 # define TLS_TCB_AT_TP 0
54 /* We use the multiple_threads field in the pthread struct */
55 #define TLS_MULTIPLE_THREADS_IN_TCB 1
57 /* Get the thread descriptor definition. */
58 # include <nptl/descr.h>
61 /* The stack_guard is accessed directly by GCC -fstack-protector code,
62 so it is a part of public ABI. The dtv and pointer_guard fields
66 /* Indicate if HTM capable (ISA 2.07). */
68 /* Reservation for Dynamic System Optimizer ABI. */
71 /* Reservation for tar register (ISA 2.07). */
73 /* GCC split stack support. */
75 /* Reservation for the Event-Based Branching ABI. */
76 uintptr_t ebb_handler;
77 uintptr_t ebb_ctx_pointer;
78 uintptr_t ebb_reserved1;
79 uintptr_t ebb_reserved2;
80 uintptr_t pointer_guard;
81 uintptr_t stack_guard;
85 /* This is the size of the initial TCB. */
86 # define TLS_INIT_TCB_SIZE 0
88 /* Alignment requirements for the initial TCB. */
89 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
91 /* This is the size of the TCB. */
92 # define TLS_TCB_SIZE 0
94 /* Alignment requirements for the TCB. */
95 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
97 /* This is the size we need before TCB. */
98 # define TLS_PRE_TCB_SIZE \
99 (sizeof (struct pthread) \
100 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
102 # ifndef __powerpc64__
103 /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
104 register void *__thread_register __asm__ ("r2");
105 # define PT_THREAD_POINTER PT_R2
107 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
108 register void *__thread_register __asm__ ("r13");
109 # define PT_THREAD_POINTER PT_R13
112 /* The following assumes that TP (R2 or R13) points to the end of the
113 TCB + 0x7000 (per the ABI). This implies that TCB address is
114 TP - 0x7000. As we define TLS_DTV_AT_TP we can
115 assume that the pthread struct is allocated immediately ahead of the
116 TCB. This implies that the pthread_descr address is
117 TP - (TLS_PRE_TCB_SIZE + 0x7000). */
118 # define TLS_TCB_OFFSET 0x7000
120 /* Install the dtv pointer. The pointer passed is to the element with
121 index -1 which contain the length. */
122 # define INSTALL_DTV(tcbp, dtvp) \
123 ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
125 /* Install new dtv for current thread. */
126 # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
128 /* Return dtv of given thread descriptor. */
129 # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
131 /* Code to initially initialize the thread pointer. This might need
132 special attention since 'errno' is not yet available and if the
133 operation can cause a failure 'errno' must not be touched. */
134 # define TLS_INIT_TP(tcbp) \
136 __thread_register = (void *) (tcbp) + TLS_TCB_OFFSET; \
137 THREAD_SET_TM_CAPABLE (GLRO (dl_hwcap2) & PPC_FEATURE2_HAS_HTM ? 1 : 0); \
141 /* Value passed to 'clone' for initialization of the thread register. */
142 # define TLS_DEFINE_INIT_TP(tp, pd) \
143 void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE; \
144 (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].tm_capable) = \
145 THREAD_GET_TM_CAPABLE ();
147 /* Return the address of the dtv for the current thread. */
148 # define THREAD_DTV() \
149 (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
151 /* Return the thread descriptor for the current thread. */
152 # define THREAD_SELF \
153 ((struct pthread *) (__thread_register \
154 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
156 /* Magic for libthread_db to know how to do THREAD_SELF. */
157 # define DB_THREAD_SELF \
158 REGISTER (32, 32, PT_THREAD_POINTER * 4, \
159 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \
160 REGISTER (64, 64, PT_THREAD_POINTER * 8, \
161 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
163 /* Read member of the thread descriptor directly. */
164 # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
166 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
167 # define THREAD_GETMEM_NC(descr, member, idx) \
168 ((void)(descr), (THREAD_SELF)->member[idx])
170 /* Set member of the thread descriptor directly. */
171 # define THREAD_SETMEM(descr, member, value) \
172 ((void)(descr), (THREAD_SELF)->member = (value))
174 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
175 # define THREAD_SETMEM_NC(descr, member, idx, value) \
176 ((void)(descr), (THREAD_SELF)->member[idx] = (value))
178 /* Set the stack guard field in TCB head. */
179 # define THREAD_SET_STACK_GUARD(value) \
180 (((tcbhead_t *) ((char *) __thread_register \
181 - TLS_TCB_OFFSET))[-1].stack_guard = (value))
182 # define THREAD_COPY_STACK_GUARD(descr) \
183 (((tcbhead_t *) ((char *) (descr) \
184 + TLS_PRE_TCB_SIZE))[-1].stack_guard \
185 = ((tcbhead_t *) ((char *) __thread_register \
186 - TLS_TCB_OFFSET))[-1].stack_guard)
188 /* Set the stack guard field in TCB head. */
189 # define THREAD_GET_POINTER_GUARD() \
190 (((tcbhead_t *) ((char *) __thread_register \
191 - TLS_TCB_OFFSET))[-1].pointer_guard)
192 # define THREAD_SET_POINTER_GUARD(value) \
193 (THREAD_GET_POINTER_GUARD () = (value))
194 # define THREAD_COPY_POINTER_GUARD(descr) \
195 (((tcbhead_t *) ((char *) (descr) \
196 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
197 = THREAD_GET_POINTER_GUARD())
199 /* tm_capable field in TCB head. */
200 # define THREAD_GET_TM_CAPABLE() \
201 (((tcbhead_t *) ((char *) __thread_register \
202 - TLS_TCB_OFFSET))[-1].tm_capable)
203 # define THREAD_SET_TM_CAPABLE(value) \
204 (THREAD_GET_TM_CAPABLE () = (value))
206 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
207 different value to mean unset l_tls_offset. */
208 # define NO_TLS_OFFSET -1
210 /* Get and set the global scope generation counter in struct pthread. */
211 #define THREAD_GSCOPE_FLAG_UNUSED 0
212 #define THREAD_GSCOPE_FLAG_USED 1
213 #define THREAD_GSCOPE_FLAG_WAIT 2
214 #define THREAD_GSCOPE_RESET_FLAG() \
217 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
218 THREAD_GSCOPE_FLAG_UNUSED); \
219 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
220 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
223 #define THREAD_GSCOPE_SET_FLAG() \
226 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
227 atomic_write_barrier (); \
230 #define THREAD_GSCOPE_WAIT() \
231 GL(dl_wait_lookup_done) ()
233 #endif /* __ASSEMBLER__ */