Use libc_hidden_* for fputs (bug 15105).
[glibc.git] / sysdeps / powerpc / nptl / tls.h
blob2418998d344eb36645908740eba991e2ce5e9c09
1 /* Definition for thread-local data handling. NPTL/PowerPC version.
2 Copyright (C) 2003-2018 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/>. */
19 #ifndef _TLS_H
20 #define _TLS_H 1
22 # include <dl-sysdep.h>
24 #ifndef __ASSEMBLER__
25 # include <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
28 # include <dl-dtv.h>
30 #else /* __ASSEMBLER__ */
31 # include <tcb-offsets.h>
32 #endif /* __ASSEMBLER__ */
35 #ifndef __ASSEMBLER__
37 # include <hwcapinfo.h>
39 /* Get system call information. */
40 # include <sysdep.h>
42 /* The TP points to the start of the thread blocks. */
43 # define TLS_DTV_AT_TP 1
44 # define TLS_TCB_AT_TP 0
46 /* We use the multiple_threads field in the pthread struct */
47 #define TLS_MULTIPLE_THREADS_IN_TCB 1
49 /* Get the thread descriptor definition. */
50 # include <nptl/descr.h>
53 /* The stack_guard is accessed directly by GCC -fstack-protector code,
54 so it is a part of public ABI. The dtv and pointer_guard fields
55 are private. */
56 typedef struct
58 /* Reservation for HWCAP data. To be accessed by GCC in
59 __builtin_cpu_supports(), so it is a part of public ABI. */
60 uint64_t hwcap;
61 /* Reservation for AT_PLATFORM data. To be accessed by GCC in
62 __builtin_cpu_is(), so it is a part of public ABI. Since there
63 are different ABIs for 32 and 64 bit, we put this field in a
64 previously empty padding space for powerpc64. */
65 #ifndef __powerpc64__
66 /* Padding to maintain alignment. */
67 uint32_t padding;
68 uint32_t at_platform;
69 #endif
70 /* Indicate if HTM capable (ISA 2.07). */
71 uint32_t tm_capable;
72 /* Reservation for AT_PLATFORM data - powerpc64. */
73 #ifdef __powerpc64__
74 uint32_t at_platform;
75 #endif
76 /* Reservation for Dynamic System Optimizer ABI. */
77 uintptr_t dso_slot2;
78 uintptr_t dso_slot1;
79 /* Reservation for tar register (ISA 2.07). */
80 uintptr_t tar_save;
81 /* GCC split stack support. */
82 void *__private_ss;
83 /* Reservation for the Event-Based Branching ABI. */
84 uintptr_t ebb_handler;
85 uintptr_t ebb_ctx_pointer;
86 uintptr_t ebb_reserved1;
87 uintptr_t ebb_reserved2;
88 uintptr_t pointer_guard;
89 uintptr_t stack_guard;
90 dtv_t *dtv;
91 } tcbhead_t;
93 /* This is the size of the initial TCB. */
94 # define TLS_INIT_TCB_SIZE 0
96 /* Alignment requirements for the initial TCB. */
97 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
99 /* This is the size of the TCB. */
100 # define TLS_TCB_SIZE 0
102 /* Alignment requirements for the TCB. */
103 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
105 /* This is the size we need before TCB. */
106 # define TLS_PRE_TCB_SIZE \
107 (sizeof (struct pthread) \
108 + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
110 # ifndef __powerpc64__
111 /* Register r2 (tp) is reserved by the ABI as "thread pointer". */
112 register void *__thread_register __asm__ ("r2");
113 # define PT_THREAD_POINTER PT_R2
114 # else
115 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
116 register void *__thread_register __asm__ ("r13");
117 # define PT_THREAD_POINTER PT_R13
118 # endif
120 /* The following assumes that TP (R2 or R13) points to the end of the
121 TCB + 0x7000 (per the ABI). This implies that TCB address is
122 TP - 0x7000. As we define TLS_DTV_AT_TP we can
123 assume that the pthread struct is allocated immediately ahead of the
124 TCB. This implies that the pthread_descr address is
125 TP - (TLS_PRE_TCB_SIZE + 0x7000). */
126 # define TLS_TCB_OFFSET 0x7000
128 /* Install the dtv pointer. The pointer passed is to the element with
129 index -1 which contain the length. */
130 # define INSTALL_DTV(tcbp, dtvp) \
131 ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1
133 /* Install new dtv for current thread. */
134 # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv))
136 /* Return dtv of given thread descriptor. */
137 # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv)
139 /* Code to initially initialize the thread pointer. This might need
140 special attention since 'errno' is not yet available and if the
141 operation can cause a failure 'errno' must not be touched. */
142 # define TLS_INIT_TP(tcbp) \
143 ({ \
144 __thread_register = (void *) (tcbp) + TLS_TCB_OFFSET; \
145 THREAD_SET_TM_CAPABLE (__tcb_hwcap & PPC_FEATURE2_HAS_HTM ? 1 : 0); \
146 THREAD_SET_HWCAP (__tcb_hwcap); \
147 THREAD_SET_AT_PLATFORM (__tcb_platform); \
148 NULL; \
151 /* Value passed to 'clone' for initialization of the thread register. */
152 # define TLS_DEFINE_INIT_TP(tp, pd) \
153 void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE; \
154 (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].tm_capable) = \
155 THREAD_GET_TM_CAPABLE (); \
156 (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].hwcap) = \
157 THREAD_GET_HWCAP (); \
158 (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].at_platform) = \
159 THREAD_GET_AT_PLATFORM ();
161 /* Return the address of the dtv for the current thread. */
162 # define THREAD_DTV() \
163 (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv)
165 /* Return the thread descriptor for the current thread. */
166 # define THREAD_SELF \
167 ((struct pthread *) (__thread_register \
168 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
170 /* Magic for libthread_db to know how to do THREAD_SELF. */
171 # define DB_THREAD_SELF \
172 REGISTER (32, 32, PT_THREAD_POINTER * 4, \
173 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \
174 REGISTER (64, 64, PT_THREAD_POINTER * 8, \
175 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)
177 /* Read member of the thread descriptor directly. */
178 # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member)
180 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
181 # define THREAD_GETMEM_NC(descr, member, idx) \
182 ((void)(descr), (THREAD_SELF)->member[idx])
184 /* Set member of the thread descriptor directly. */
185 # define THREAD_SETMEM(descr, member, value) \
186 ((void)(descr), (THREAD_SELF)->member = (value))
188 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
189 # define THREAD_SETMEM_NC(descr, member, idx, value) \
190 ((void)(descr), (THREAD_SELF)->member[idx] = (value))
192 /* Set the stack guard field in TCB head. */
193 # define THREAD_SET_STACK_GUARD(value) \
194 (((tcbhead_t *) ((char *) __thread_register \
195 - TLS_TCB_OFFSET))[-1].stack_guard = (value))
196 # define THREAD_COPY_STACK_GUARD(descr) \
197 (((tcbhead_t *) ((char *) (descr) \
198 + TLS_PRE_TCB_SIZE))[-1].stack_guard \
199 = ((tcbhead_t *) ((char *) __thread_register \
200 - TLS_TCB_OFFSET))[-1].stack_guard)
202 /* Set the stack guard field in TCB head. */
203 # define THREAD_GET_POINTER_GUARD() \
204 (((tcbhead_t *) ((char *) __thread_register \
205 - TLS_TCB_OFFSET))[-1].pointer_guard)
206 # define THREAD_SET_POINTER_GUARD(value) \
207 (THREAD_GET_POINTER_GUARD () = (value))
208 # define THREAD_COPY_POINTER_GUARD(descr) \
209 (((tcbhead_t *) ((char *) (descr) \
210 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \
211 = THREAD_GET_POINTER_GUARD())
213 /* tm_capable field in TCB head. */
214 # define THREAD_GET_TM_CAPABLE() \
215 (((tcbhead_t *) ((char *) __thread_register \
216 - TLS_TCB_OFFSET))[-1].tm_capable)
217 # define THREAD_SET_TM_CAPABLE(value) \
218 (THREAD_GET_TM_CAPABLE () = (value))
220 /* hwcap field in TCB head. */
221 # define THREAD_GET_HWCAP() \
222 (((tcbhead_t *) ((char *) __thread_register \
223 - TLS_TCB_OFFSET))[-1].hwcap)
224 # define THREAD_SET_HWCAP(value) \
225 (THREAD_GET_HWCAP () = (value))
227 /* at_platform field in TCB head. */
228 # define THREAD_GET_AT_PLATFORM() \
229 (((tcbhead_t *) ((char *) __thread_register \
230 - TLS_TCB_OFFSET))[-1].at_platform)
231 # define THREAD_SET_AT_PLATFORM(value) \
232 (THREAD_GET_AT_PLATFORM () = (value))
234 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some
235 different value to mean unset l_tls_offset. */
236 # define NO_TLS_OFFSET -1
238 /* Get and set the global scope generation counter in struct pthread. */
239 #define THREAD_GSCOPE_FLAG_UNUSED 0
240 #define THREAD_GSCOPE_FLAG_USED 1
241 #define THREAD_GSCOPE_FLAG_WAIT 2
242 #define THREAD_GSCOPE_RESET_FLAG() \
243 do \
244 { int __res \
245 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \
246 THREAD_GSCOPE_FLAG_UNUSED); \
247 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
248 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
250 while (0)
251 #define THREAD_GSCOPE_SET_FLAG() \
252 do \
254 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \
255 atomic_write_barrier (); \
257 while (0)
258 #define THREAD_GSCOPE_WAIT() \
259 GL(dl_wait_lookup_done) ()
261 #endif /* __ASSEMBLER__ */
263 #endif /* tls.h */