Support multiarch for i686.
[glibc.git] / nptl / sysdeps / x86_64 / tls.h
blob4212038ab507b1490906f2ee2c940cac0e103651
1 /* Definition for thread-local data handling. nptl/x86_64 version.
2 Copyright (C) 2002-2007, 2008, 2009 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _TLS_H
21 #define _TLS_H 1
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 # include <sysdep.h>
30 # include <kernel-features.h>
31 # include <bits/wordsize.h>
32 # include <xmmintrin.h>
35 /* Type for the dtv. */
36 typedef union dtv
38 size_t counter;
39 struct
41 void *val;
42 bool is_static;
43 } pointer;
44 } dtv_t;
47 typedef struct
49 void *tcb; /* Pointer to the TCB. Not necessarily the
50 thread descriptor used by libpthread. */
51 dtv_t *dtv;
52 void *self; /* Pointer to the thread descriptor. */
53 int multiple_threads;
54 int gscope_flag;
55 uintptr_t sysinfo;
56 uintptr_t stack_guard;
57 uintptr_t pointer_guard;
58 unsigned long int vgetcpu_cache[2];
59 # ifndef __ASSUME_PRIVATE_FUTEX
60 int private_futex;
61 # else
62 int __unused1;
63 # endif
64 # if __WORDSIZE == 64
65 int rtld_must_xmm_save;
66 # endif
67 /* Reservation of some values for the TM ABI. */
68 void *__private_tm[5];
69 # if __WORDSIZE == 64
70 long int __unused2;
71 /* Have space for the post-AVX register size. */
72 __m128 rtld_savespace_sse[8][4];
74 void *__padding[8];
75 # endif
76 } tcbhead_t;
78 #else /* __ASSEMBLER__ */
79 # include <tcb-offsets.h>
80 #endif
83 /* We require TLS support in the tools. */
84 #ifndef HAVE_TLS_SUPPORT
85 # error "TLS support is required."
86 #endif
88 /* Alignment requirement for the stack. */
89 #define STACK_ALIGN 16
92 #ifndef __ASSEMBLER__
93 /* Get system call information. */
94 # include <sysdep.h>
97 /* Get the thread descriptor definition. */
98 # include <nptl/descr.h>
100 #ifndef LOCK_PREFIX
101 # ifdef UP
102 # define LOCK_PREFIX /* nothing */
103 # else
104 # define LOCK_PREFIX "lock;"
105 # endif
106 #endif
108 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
109 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
110 struct pthread even when not linked with -lpthread. */
111 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
113 /* Alignment requirements for the initial TCB. */
114 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
116 /* This is the size of the TCB. */
117 # define TLS_TCB_SIZE sizeof (struct pthread)
119 /* Alignment requirements for the TCB. */
120 //# define TLS_TCB_ALIGN __alignof__ (struct pthread)
121 // Normally the above would be correct But we have to store post-AVX
122 // vector registers in the TCB and we want the storage to be aligned.
123 // unfortunately there isn't yet a type for these values and hence no
124 // 32-byte alignment requirement. Make this explicit, for now.
125 # define TLS_TCB_ALIGN 32
127 /* The TCB can have any size and the memory following the address the
128 thread pointer points to is unspecified. Allocate the TCB there. */
129 # define TLS_TCB_AT_TP 1
132 /* Install the dtv pointer. The pointer passed is to the element with
133 index -1 which contain the length. */
134 # define INSTALL_DTV(descr, dtvp) \
135 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
137 /* Install new dtv for current thread. */
138 # define INSTALL_NEW_DTV(dtvp) \
139 ({ struct pthread *__pd; \
140 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
142 /* Return dtv of given thread descriptor. */
143 # define GET_DTV(descr) \
144 (((tcbhead_t *) (descr))->dtv)
147 /* Macros to load from and store into segment registers. */
148 # define TLS_GET_FS() \
149 ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
150 # define TLS_SET_FS(val) \
151 __asm ("movl %0, %%fs" :: "q" (val))
154 /* Code to initially initialize the thread pointer. This might need
155 special attention since 'errno' is not yet available and if the
156 operation can cause a failure 'errno' must not be touched.
158 We have to make the syscall for both uses of the macro since the
159 address might be (and probably is) different. */
160 # define TLS_INIT_TP(thrdescr, secondcall) \
161 ({ void *_thrdescr = (thrdescr); \
162 tcbhead_t *_head = _thrdescr; \
163 int _result; \
165 _head->tcb = _thrdescr; \
166 /* For now the thread descriptor is at the same address. */ \
167 _head->self = _thrdescr; \
169 /* It is a simple syscall to set the %fs value for the thread. */ \
170 asm volatile ("syscall" \
171 : "=a" (_result) \
172 : "0" ((unsigned long int) __NR_arch_prctl), \
173 "D" ((unsigned long int) ARCH_SET_FS), \
174 "S" (_thrdescr) \
175 : "memory", "cc", "r11", "cx"); \
177 _result ? "cannot set %fs base address for thread-local storage" : 0; \
181 /* Return the address of the dtv for the current thread. */
182 # define THREAD_DTV() \
183 ({ struct pthread *__pd; \
184 THREAD_GETMEM (__pd, header.dtv); })
187 /* Return the thread descriptor for the current thread.
189 The contained asm must *not* be marked volatile since otherwise
190 assignments like
191 pthread_descr self = thread_self();
192 do not get optimized away. */
193 # define THREAD_SELF \
194 ({ struct pthread *__self; \
195 asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
196 : "i" (offsetof (struct pthread, header.self))); \
197 __self;})
199 /* Magic for libthread_db to know how to do THREAD_SELF. */
200 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
201 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
203 /* Read member of the thread descriptor directly. */
204 # define THREAD_GETMEM(descr, member) \
205 ({ __typeof (descr->member) __value; \
206 if (sizeof (__value) == 1) \
207 asm volatile ("movb %%fs:%P2,%b0" \
208 : "=q" (__value) \
209 : "0" (0), "i" (offsetof (struct pthread, member))); \
210 else if (sizeof (__value) == 4) \
211 asm volatile ("movl %%fs:%P1,%0" \
212 : "=r" (__value) \
213 : "i" (offsetof (struct pthread, member))); \
214 else \
216 if (sizeof (__value) != 8) \
217 /* There should not be any value with a size other than 1, \
218 4 or 8. */ \
219 abort (); \
221 asm volatile ("movq %%fs:%P1,%q0" \
222 : "=r" (__value) \
223 : "i" (offsetof (struct pthread, member))); \
225 __value; })
228 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
229 # define THREAD_GETMEM_NC(descr, member, idx) \
230 ({ __typeof (descr->member[0]) __value; \
231 if (sizeof (__value) == 1) \
232 asm volatile ("movb %%fs:%P2(%q3),%b0" \
233 : "=q" (__value) \
234 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
235 "r" (idx)); \
236 else if (sizeof (__value) == 4) \
237 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
238 : "=r" (__value) \
239 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
240 else \
242 if (sizeof (__value) != 8) \
243 /* There should not be any value with a size other than 1, \
244 4 or 8. */ \
245 abort (); \
247 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
248 : "=r" (__value) \
249 : "i" (offsetof (struct pthread, member[0])), \
250 "r" (idx)); \
252 __value; })
255 /* Loading addresses of objects on x86-64 needs to be treated special
256 when generating PIC code. */
257 #ifdef __pic__
258 # define IMM_MODE "nr"
259 #else
260 # define IMM_MODE "ir"
261 #endif
264 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
265 # define THREAD_SETMEM(descr, member, value) \
266 ({ if (sizeof (descr->member) == 1) \
267 asm volatile ("movb %b0,%%fs:%P1" : \
268 : "iq" (value), \
269 "i" (offsetof (struct pthread, member))); \
270 else if (sizeof (descr->member) == 4) \
271 asm volatile ("movl %0,%%fs:%P1" : \
272 : IMM_MODE (value), \
273 "i" (offsetof (struct pthread, member))); \
274 else \
276 if (sizeof (descr->member) != 8) \
277 /* There should not be any value with a size other than 1, \
278 4 or 8. */ \
279 abort (); \
281 asm volatile ("movq %q0,%%fs:%P1" : \
282 : IMM_MODE ((unsigned long int) value), \
283 "i" (offsetof (struct pthread, member))); \
287 /* Set member of the thread descriptor directly. */
288 # define THREAD_SETMEM_NC(descr, member, idx, value) \
289 ({ if (sizeof (descr->member[0]) == 1) \
290 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
291 : "iq" (value), \
292 "i" (offsetof (struct pthread, member[0])), \
293 "r" (idx)); \
294 else if (sizeof (descr->member[0]) == 4) \
295 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
296 : IMM_MODE (value), \
297 "i" (offsetof (struct pthread, member[0])), \
298 "r" (idx)); \
299 else \
301 if (sizeof (descr->member[0]) != 8) \
302 /* There should not be any value with a size other than 1, \
303 4 or 8. */ \
304 abort (); \
306 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
307 : IMM_MODE ((unsigned long int) value), \
308 "i" (offsetof (struct pthread, member[0])), \
309 "r" (idx)); \
313 /* Atomic compare and exchange on TLS, returning old value. */
314 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
315 ({ __typeof (descr->member) __ret; \
316 __typeof (oldval) __old = (oldval); \
317 if (sizeof (descr->member) == 4) \
318 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
319 : "=a" (__ret) \
320 : "0" (__old), "r" (newval), \
321 "i" (offsetof (struct pthread, member))); \
322 else \
323 /* Not necessary for other sizes in the moment. */ \
324 abort (); \
325 __ret; })
328 /* Atomic logical and. */
329 # define THREAD_ATOMIC_AND(descr, member, val) \
330 (void) ({ if (sizeof ((descr)->member) == 4) \
331 asm volatile (LOCK_PREFIX "andl %1, %%fs:%P0" \
332 :: "i" (offsetof (struct pthread, member)), \
333 "ir" (val)); \
334 else \
335 /* Not necessary for other sizes in the moment. */ \
336 abort (); })
339 /* Atomic set bit. */
340 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
341 (void) ({ if (sizeof ((descr)->member) == 4) \
342 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
343 :: "i" (offsetof (struct pthread, member)), \
344 "ir" (1 << (bit))); \
345 else \
346 /* Not necessary for other sizes in the moment. */ \
347 abort (); })
350 # define CALL_THREAD_FCT(descr) \
351 ({ void *__res; \
352 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
353 "callq *%%fs:%P1" \
354 : "=a" (__res) \
355 : "i" (offsetof (struct pthread, start_routine)), \
356 "i" (offsetof (struct pthread, arg)) \
357 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
358 "memory", "cc"); \
359 __res; })
362 /* Set the stack guard field in TCB head. */
363 # define THREAD_SET_STACK_GUARD(value) \
364 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
365 # define THREAD_COPY_STACK_GUARD(descr) \
366 ((descr)->header.stack_guard \
367 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
370 /* Set the pointer guard field in the TCB head. */
371 # define THREAD_SET_POINTER_GUARD(value) \
372 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
373 # define THREAD_COPY_POINTER_GUARD(descr) \
374 ((descr)->header.pointer_guard \
375 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
378 /* Get and set the global scope generation counter in the TCB head. */
379 # define THREAD_GSCOPE_FLAG_UNUSED 0
380 # define THREAD_GSCOPE_FLAG_USED 1
381 # define THREAD_GSCOPE_FLAG_WAIT 2
382 # define THREAD_GSCOPE_RESET_FLAG() \
383 do \
384 { int __res; \
385 asm volatile ("xchgl %0, %%fs:%P1" \
386 : "=r" (__res) \
387 : "i" (offsetof (struct pthread, header.gscope_flag)), \
388 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
389 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
390 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
392 while (0)
393 # define THREAD_GSCOPE_SET_FLAG() \
394 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
395 # define THREAD_GSCOPE_WAIT() \
396 GL(dl_wait_lookup_done) ()
399 # ifdef SHARED
400 /* Defined in dl-trampoline.S. */
401 extern void _dl_x86_64_save_sse (void);
402 extern void _dl_x86_64_restore_sse (void);
404 # define RTLD_CHECK_FOREIGN_CALL \
405 (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
407 # define RTLD_ENABLE_FOREIGN_CALL \
408 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 1)
410 # define RTLD_PREPARE_FOREIGN_CALL \
411 do if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save)) \
413 _dl_x86_64_save_sse (); \
414 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0); \
416 while (0)
418 # define RTLD_FINALIZE_FOREIGN_CALL \
419 do { \
420 if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) == 0) \
421 _dl_x86_64_restore_sse (); \
422 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0); \
423 } while (0)
424 # endif
427 #endif /* __ASSEMBLER__ */
429 #endif /* tls.h */