]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/sysdeps/x86_64/tls.h
.
[thirdparty/glibc.git] / nptl / sysdeps / x86_64 / tls.h
1 /* Definition for thread-local data handling. nptl/x86_64 version.
2 Copyright (C) 2002,2003,2004,2005,2006,2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #ifndef _TLS_H
21 #define _TLS_H 1
22
23 #ifndef __ASSEMBLER__
24 # include <asm/prctl.h> /* For ARCH_SET_FS. */
25 # include <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
28 # include <stdlib.h>
29
30
31 /* Type for the dtv. */
32 typedef union dtv
33 {
34 size_t counter;
35 struct
36 {
37 void *val;
38 bool is_static;
39 } pointer;
40 } dtv_t;
41
42
43 typedef struct
44 {
45 void *tcb; /* Pointer to the TCB. Not necessary the
46 thread descriptor used by libpthread. */
47 dtv_t *dtv;
48 void *self; /* Pointer to the thread descriptor. */
49 int multiple_threads;
50 int gscope_flag;
51 uintptr_t sysinfo;
52 uintptr_t stack_guard;
53 uintptr_t pointer_guard;
54 } tcbhead_t;
55
56 #else /* __ASSEMBLER__ */
57 # include <tcb-offsets.h>
58 #endif
59
60
61 /* We require TLS support in the tools. */
62 #ifndef HAVE_TLS_SUPPORT
63 # error "TLS support is required."
64 #endif
65
66 /* Signal that TLS support is available. */
67 #define USE_TLS 1
68
69 /* Alignment requirement for the stack. */
70 #define STACK_ALIGN 16
71
72
73 #ifndef __ASSEMBLER__
74 /* Get system call information. */
75 # include <sysdep.h>
76
77
78 /* Get the thread descriptor definition. */
79 # include <nptl/descr.h>
80
81 #ifndef LOCK_PREFIX
82 # ifdef UP
83 # define LOCK_PREFIX /* nothing */
84 # else
85 # define LOCK_PREFIX "lock;"
86 # endif
87 #endif
88
89 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
90 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
91 struct pthread even when not linked with -lpthread. */
92 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
93
94 /* Alignment requirements for the initial TCB. */
95 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
96
97 /* This is the size of the TCB. */
98 # define TLS_TCB_SIZE sizeof (struct pthread)
99
100 /* Alignment requirements for the TCB. */
101 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
102
103 /* The TCB can have any size and the memory following the address the
104 thread pointer points to is unspecified. Allocate the TCB there. */
105 # define TLS_TCB_AT_TP 1
106
107
108 /* Install the dtv pointer. The pointer passed is to the element with
109 index -1 which contain the length. */
110 # define INSTALL_DTV(descr, dtvp) \
111 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
112
113 /* Install new dtv for current thread. */
114 # define INSTALL_NEW_DTV(dtvp) \
115 ({ struct pthread *__pd; \
116 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
117
118 /* Return dtv of given thread descriptor. */
119 # define GET_DTV(descr) \
120 (((tcbhead_t *) (descr))->dtv)
121
122
123 /* Macros to load from and store into segment registers. */
124 # define TLS_GET_FS() \
125 ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
126 # define TLS_SET_FS(val) \
127 __asm ("movl %0, %%fs" :: "q" (val))
128
129
130 /* Code to initially initialize the thread pointer. This might need
131 special attention since 'errno' is not yet available and if the
132 operation can cause a failure 'errno' must not be touched.
133
134 We have to make the syscall for both uses of the macro since the
135 address might be (and probably is) different. */
136 # define TLS_INIT_TP(thrdescr, secondcall) \
137 ({ void *_thrdescr = (thrdescr); \
138 tcbhead_t *_head = _thrdescr; \
139 int _result; \
140 \
141 _head->tcb = _thrdescr; \
142 /* For now the thread descriptor is at the same address. */ \
143 _head->self = _thrdescr; \
144 \
145 /* It is a simple syscall to set the %fs value for the thread. */ \
146 asm volatile ("syscall" \
147 : "=a" (_result) \
148 : "0" ((unsigned long int) __NR_arch_prctl), \
149 "D" ((unsigned long int) ARCH_SET_FS), \
150 "S" (_thrdescr) \
151 : "memory", "cc", "r11", "cx"); \
152 \
153 _result ? "cannot set %fs base address for thread-local storage" : 0; \
154 })
155
156
157 /* Return the address of the dtv for the current thread. */
158 # define THREAD_DTV() \
159 ({ struct pthread *__pd; \
160 THREAD_GETMEM (__pd, header.dtv); })
161
162
163 /* Return the thread descriptor for the current thread.
164
165 The contained asm must *not* be marked volatile since otherwise
166 assignments like
167 pthread_descr self = thread_self();
168 do not get optimized away. */
169 # define THREAD_SELF \
170 ({ struct pthread *__self; \
171 asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
172 : "i" (offsetof (struct pthread, header.self))); \
173 __self;})
174
175 /* Magic for libthread_db to know how to do THREAD_SELF. */
176 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
177 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
178
179 /* Read member of the thread descriptor directly. */
180 # define THREAD_GETMEM(descr, member) \
181 ({ __typeof (descr->member) __value; \
182 if (sizeof (__value) == 1) \
183 asm volatile ("movb %%fs:%P2,%b0" \
184 : "=q" (__value) \
185 : "0" (0), "i" (offsetof (struct pthread, member))); \
186 else if (sizeof (__value) == 4) \
187 asm volatile ("movl %%fs:%P1,%0" \
188 : "=r" (__value) \
189 : "i" (offsetof (struct pthread, member))); \
190 else \
191 { \
192 if (sizeof (__value) != 8) \
193 /* There should not be any value with a size other than 1, \
194 4 or 8. */ \
195 abort (); \
196 \
197 asm volatile ("movq %%fs:%P1,%q0" \
198 : "=r" (__value) \
199 : "i" (offsetof (struct pthread, member))); \
200 } \
201 __value; })
202
203
204 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
205 # define THREAD_GETMEM_NC(descr, member, idx) \
206 ({ __typeof (descr->member[0]) __value; \
207 if (sizeof (__value) == 1) \
208 asm volatile ("movb %%fs:%P2(%q3),%b0" \
209 : "=q" (__value) \
210 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
211 "r" (idx)); \
212 else if (sizeof (__value) == 4) \
213 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
214 : "=r" (__value) \
215 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
216 else \
217 { \
218 if (sizeof (__value) != 8) \
219 /* There should not be any value with a size other than 1, \
220 4 or 8. */ \
221 abort (); \
222 \
223 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
224 : "=r" (__value) \
225 : "i" (offsetof (struct pthread, member[0])), \
226 "r" (idx)); \
227 } \
228 __value; })
229
230
231 /* Loading addresses of objects on x86-64 needs to be treated special
232 when generating PIC code. */
233 #ifdef __pic__
234 # define IMM_MODE "nr"
235 #else
236 # define IMM_MODE "ir"
237 #endif
238
239
240 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
241 # define THREAD_SETMEM(descr, member, value) \
242 ({ if (sizeof (descr->member) == 1) \
243 asm volatile ("movb %b0,%%fs:%P1" : \
244 : "iq" (value), \
245 "i" (offsetof (struct pthread, member))); \
246 else if (sizeof (descr->member) == 4) \
247 asm volatile ("movl %0,%%fs:%P1" : \
248 : IMM_MODE (value), \
249 "i" (offsetof (struct pthread, member))); \
250 else \
251 { \
252 if (sizeof (descr->member) != 8) \
253 /* There should not be any value with a size other than 1, \
254 4 or 8. */ \
255 abort (); \
256 \
257 asm volatile ("movq %q0,%%fs:%P1" : \
258 : IMM_MODE ((unsigned long int) value), \
259 "i" (offsetof (struct pthread, member))); \
260 }})
261
262
263 /* Set member of the thread descriptor directly. */
264 # define THREAD_SETMEM_NC(descr, member, idx, value) \
265 ({ if (sizeof (descr->member[0]) == 1) \
266 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
267 : "iq" (value), \
268 "i" (offsetof (struct pthread, member[0])), \
269 "r" (idx)); \
270 else if (sizeof (descr->member[0]) == 4) \
271 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
272 : IMM_MODE (value), \
273 "i" (offsetof (struct pthread, member[0])), \
274 "r" (idx)); \
275 else \
276 { \
277 if (sizeof (descr->member[0]) != 8) \
278 /* There should not be any value with a size other than 1, \
279 4 or 8. */ \
280 abort (); \
281 \
282 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
283 : IMM_MODE ((unsigned long int) value), \
284 "i" (offsetof (struct pthread, member[0])), \
285 "r" (idx)); \
286 }})
287
288
289 /* Atomic compare and exchange on TLS, returning old value. */
290 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
291 ({ __typeof (descr->member) __ret; \
292 __typeof (oldval) __old = (oldval); \
293 if (sizeof (descr->member) == 4) \
294 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
295 : "=a" (__ret) \
296 : "0" (__old), "r" (newval), \
297 "i" (offsetof (struct pthread, member))); \
298 else \
299 /* Not necessary for other sizes in the moment. */ \
300 abort (); \
301 __ret; })
302
303
304 /* Atomic set bit. */
305 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
306 (void) ({ if (sizeof ((descr)->member) == 4) \
307 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
308 :: "i" (offsetof (struct pthread, member)), \
309 "ir" (1 << (bit))); \
310 else \
311 /* Not necessary for other sizes in the moment. */ \
312 abort (); })
313
314
315 #define CALL_THREAD_FCT(descr) \
316 ({ void *__res; \
317 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
318 "callq *%%fs:%P1" \
319 : "=a" (__res) \
320 : "i" (offsetof (struct pthread, start_routine)), \
321 "i" (offsetof (struct pthread, arg)) \
322 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
323 "memory", "cc"); \
324 __res; })
325
326
327 /* Set the stack guard field in TCB head. */
328 # define THREAD_SET_STACK_GUARD(value) \
329 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
330 # define THREAD_COPY_STACK_GUARD(descr) \
331 ((descr)->header.stack_guard \
332 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
333
334
335 /* Set the pointer guard field in the TCB head. */
336 #define THREAD_SET_POINTER_GUARD(value) \
337 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
338 #define THREAD_COPY_POINTER_GUARD(descr) \
339 ((descr)->header.pointer_guard \
340 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
341
342
343 /* Get and set the global scope generation counter in the TCB head. */
344 #define THREAD_GSCOPE_FLAG_UNUSED 0
345 #define THREAD_GSCOPE_FLAG_USED 1
346 #define THREAD_GSCOPE_FLAG_WAIT 2
347 #define THREAD_GSCOPE_RESET_FLAG() \
348 do \
349 { int __res; \
350 asm volatile ("xchgl %0, %%fs:%P1" \
351 : "=r" (__res) \
352 : "i" (offsetof (struct pthread, header.gscope_flag)), \
353 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
354 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
355 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1); \
356 } \
357 while (0)
358 #define THREAD_GSCOPE_SET_FLAG() \
359 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
360 #define THREAD_GSCOPE_WAIT() \
361 GL(dl_wait_lookup_done) ()
362
363 #endif /* __ASSEMBLER__ */
364
365 #endif /* tls.h */