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
22 #if defined USE_TLS && USE_TLS // l4
24 # include <asm/prctl.h> /* For ARCH_SET_FS. */
30 # include <bits/kernel-features.h>
31 # include <bits/wordsize.h>
32 # include <xmmintrin.h>
35 /* Type for the dtv. */
49 void *tcb; /* Pointer to the TCB. Not necessarily the
50 thread descriptor used by libpthread. */
52 void *self; /* Pointer to the thread descriptor. */
56 uintptr_t stack_guard;
57 uintptr_t pointer_guard;
58 unsigned long int vgetcpu_cache[2];
59 # ifndef __ASSUME_PRIVATE_FUTEX
65 int rtld_must_xmm_save;
67 /* Reservation of some values for the TM ABI. */
68 void *__private_tm[5];
71 /* Have space for the post-AVX register size. */
72 __m128 rtld_savespace_sse[8][4];
78 #else /* __ASSEMBLER__ */
79 # include <tcb-offsets.h>
83 /* We require TLS support in the tools. */
84 #define HAVE_TLS_SUPPORT 1
85 #define HAVE___THREAD 1
86 #define HAVE_TLS_MODEL_ATTRIBUTE 1
88 /* Signal that TLS support is available. */
91 /* Alignment requirement for the stack. */
92 #define STACK_ALIGN 16
96 /* Get system call information. */
100 /* Get the thread descriptor definition. */
105 # define LOCK_PREFIX /* nothing */
107 # define LOCK_PREFIX "lock;"
111 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
112 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
113 struct pthread even when not linked with -lpthread. */
114 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
116 /* Alignment requirements for the initial TCB. */
117 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
119 /* This is the size of the TCB. */
120 # define TLS_TCB_SIZE sizeof (struct pthread)
122 /* Alignment requirements for the TCB. */
123 //# define TLS_TCB_ALIGN __alignof__ (struct pthread)
124 // Normally the above would be correct But we have to store post-AVX
125 // vector registers in the TCB and we want the storage to be aligned.
126 // unfortunately there isn't yet a type for these values and hence no
127 // 32-byte alignment requirement. Make this explicit, for now.
128 # define TLS_TCB_ALIGN 32
130 /* The TCB can have any size and the memory following the address the
131 thread pointer points to is unspecified. Allocate the TCB there. */
132 # define TLS_TCB_AT_TP 1
135 /* Install the dtv pointer. The pointer passed is to the element with
136 index -1 which contain the length. */
137 # define INSTALL_DTV(descr, dtvp) \
138 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
140 /* Install new dtv for current thread. */
141 # define INSTALL_NEW_DTV(dtvp) \
142 ({ struct pthread *__pd; \
143 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
145 /* Return dtv of given thread descriptor. */
146 # define GET_DTV(descr) \
147 (((tcbhead_t *) (descr))->dtv)
150 /* Macros to load from and store into segment registers. */
151 # define TLS_GET_FS() \
152 ({ int __seg; __asm__ ("movl %%fs, %0" : "=q" (__seg)); __seg; })
153 # define TLS_SET_FS(val) \
154 __asm__ ("movl %0, %%fs" :: "q" (val))
157 /* Code to initially initialize the thread pointer. This might need
158 special attention since 'errno' is not yet available and if the
159 operation can cause a failure 'errno' must not be touched.
161 We have to make the syscall for both uses of the macro since the
162 address might be (and probably is) different. */
163 # define TLS_INIT_TP(thrdescr, secondcall) \
164 ({ void *_thrdescr = (thrdescr); \
165 tcbhead_t *_head = _thrdescr; \
168 _head->tcb = _thrdescr; \
169 /* For now the thread descriptor is at the same address. */ \
170 _head->self = _thrdescr; \
172 /* It is a simple syscall to set the %fs value for the thread. */ \
173 __asm__ __volatile__ ("syscall" \
175 : "0" ((unsigned long int) __NR_arch_prctl), \
176 "D" ((unsigned long int) ARCH_SET_FS), \
178 : "memory", "cc", "r11", "cx"); \
180 _result ? "cannot set %fs base address for thread-local storage" : 0; \
184 /* Return the address of the dtv for the current thread. */
185 # define THREAD_DTV() \
186 ({ struct pthread *__pd; \
187 THREAD_GETMEM (__pd, header.dtv); })
190 /* Return the thread descriptor for the current thread.
192 The contained asm must *not* be marked __volatile__ since otherwise
194 pthread_descr self = thread_self();
195 do not get optimized away. */
196 # define THREAD_SELF \
197 ({ struct pthread *__self; \
198 __asm__ ("movq %%fs:%c1,%q0" : "=r" (__self) \
199 : "i" (offsetof (struct pthread, header.self))); \
202 /* Magic for libthread_db to know how to do THREAD_SELF. */
203 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
204 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
206 /* Read member of the thread descriptor directly. */
207 # define THREAD_GETMEM(descr, member) \
208 ({ __typeof (descr->member) __value; \
209 if (sizeof (__value) == 1) \
210 __asm__ __volatile__ ("movb %%fs:%P2,%b0" \
212 : "0" (0), "i" (offsetof (struct pthread, member))); \
213 else if (sizeof (__value) == 4) \
214 __asm__ __volatile__ ("movl %%fs:%P1,%0" \
216 : "i" (offsetof (struct pthread, member))); \
219 if (sizeof (__value) != 8) \
220 /* There should not be any value with a size other than 1, \
224 __asm__ __volatile__ ("movq %%fs:%P1,%q0" \
226 : "i" (offsetof (struct pthread, member))); \
231 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
232 # define THREAD_GETMEM_NC(descr, member, idx) \
233 ({ __typeof (descr->member[0]) __value; \
234 if (sizeof (__value) == 1) \
235 __asm__ __volatile__ ("movb %%fs:%P2(%q3),%b0" \
237 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
239 else if (sizeof (__value) == 4) \
240 __asm__ __volatile__ ("movl %%fs:%P1(,%q2,4),%0" \
242 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
245 if (sizeof (__value) != 8) \
246 /* There should not be any value with a size other than 1, \
250 __asm__ __volatile__ ("movq %%fs:%P1(,%q2,8),%q0" \
252 : "i" (offsetof (struct pthread, member[0])), \
258 /* Loading addresses of objects on x86-64 needs to be treated special
259 when generating PIC code. */
261 # define IMM_MODE "nr"
263 # define IMM_MODE "ir"
267 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
268 # define THREAD_SETMEM(descr, member, value) \
269 ({ if (sizeof (descr->member) == 1) \
270 __asm__ __volatile__ ("movb %b0,%%fs:%P1" : \
272 "i" (offsetof (struct pthread, member))); \
273 else if (sizeof (descr->member) == 4) \
274 __asm__ __volatile__ ("movl %0,%%fs:%P1" : \
275 : IMM_MODE (value), \
276 "i" (offsetof (struct pthread, member))); \
279 if (sizeof (descr->member) != 8) \
280 /* There should not be any value with a size other than 1, \
284 __asm__ __volatile__ ("movq %q0,%%fs:%P1" : \
285 : IMM_MODE ((unsigned long int) value), \
286 "i" (offsetof (struct pthread, member))); \
290 /* Set member of the thread descriptor directly. */
291 # define THREAD_SETMEM_NC(descr, member, idx, value) \
292 ({ if (sizeof (descr->member[0]) == 1) \
293 __asm__ __volatile__ ("movb %b0,%%fs:%P1(%q2)" : \
295 "i" (offsetof (struct pthread, member[0])), \
297 else if (sizeof (descr->member[0]) == 4) \
298 __asm__ __volatile__ ("movl %0,%%fs:%P1(,%q2,4)" : \
299 : IMM_MODE (value), \
300 "i" (offsetof (struct pthread, member[0])), \
304 if (sizeof (descr->member[0]) != 8) \
305 /* There should not be any value with a size other than 1, \
309 __asm__ __volatile__ ("movq %q0,%%fs:%P1(,%q2,8)" : \
310 : IMM_MODE ((unsigned long int) value), \
311 "i" (offsetof (struct pthread, member[0])), \
316 /* Atomic compare and exchange on TLS, returning old value. */
317 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
318 ({ __typeof (descr->member) __ret; \
319 __typeof (oldval) __old = (oldval); \
320 if (sizeof (descr->member) == 4) \
321 __asm__ __volatile__ (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
323 : "0" (__old), "r" (newval), \
324 "i" (offsetof (struct pthread, member))); \
326 /* Not necessary for other sizes in the moment. */ \
331 /* Atomic logical and. */
332 # define THREAD_ATOMIC_AND(descr, member, val) \
333 (void) ({ if (sizeof ((descr)->member) == 4) \
334 __asm__ __volatile__ (LOCK_PREFIX "andl %1, %%fs:%P0" \
335 :: "i" (offsetof (struct pthread, member)), \
338 /* Not necessary for other sizes in the moment. */ \
342 /* Atomic set bit. */
343 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
344 (void) ({ if (sizeof ((descr)->member) == 4) \
345 __asm__ __volatile__ (LOCK_PREFIX "orl %1, %%fs:%P0" \
346 :: "i" (offsetof (struct pthread, member)), \
347 "ir" (1 << (bit))); \
349 /* Not necessary for other sizes in the moment. */ \
353 # define CALL_THREAD_FCT(descr) \
355 __asm__ __volatile__ ("movq %%fs:%P2, %%rdi\n\t" \
358 : "i" (offsetof (struct pthread, start_routine)), \
359 "i" (offsetof (struct pthread, arg)) \
360 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
365 /* Set the stack guard field in TCB head. */
366 # define THREAD_SET_STACK_GUARD(value) \
367 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
368 # define THREAD_COPY_STACK_GUARD(descr) \
369 ((descr)->header.stack_guard \
370 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
373 /* Set the pointer guard field in the TCB head. */
374 # define THREAD_SET_POINTER_GUARD(value) \
375 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
376 # define THREAD_COPY_POINTER_GUARD(descr) \
377 ((descr)->header.pointer_guard \
378 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
381 /* Get and set the global scope generation counter in the TCB head. */
382 # define THREAD_GSCOPE_FLAG_UNUSED 0
383 # define THREAD_GSCOPE_FLAG_USED 1
384 # define THREAD_GSCOPE_FLAG_WAIT 2
385 # define THREAD_GSCOPE_RESET_FLAG() \
388 __asm__ __volatile__ ("xchgl %0, %%fs:%P1" \
390 : "i" (offsetof (struct pthread, header.gscope_flag)), \
391 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
392 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
393 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
396 # define THREAD_GSCOPE_SET_FLAG() \
397 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
398 # define THREAD_GSCOPE_WAIT() \
399 GL(dl_wait_lookup_done) ()
403 /* Defined in dl-trampoline.S. */
404 extern void _dl_x86_64_save_sse (void);
405 extern void _dl_x86_64_restore_sse (void);
407 # define RTLD_CHECK_FOREIGN_CALL \
408 (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
410 /* NB: Don't use the xchg operation because that would imply a lock
411 prefix which is expensive and unnecessary. The cache line is also
412 not contested at all. */
413 # define RTLD_ENABLE_FOREIGN_CALL \
414 int old_rtld_must_xmm_save = THREAD_GETMEM (THREAD_SELF, \
415 header.rtld_must_xmm_save); \
416 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 1)
418 # define RTLD_PREPARE_FOREIGN_CALL \
419 do if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save)) \
421 _dl_x86_64_save_sse (); \
422 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0); \
426 # define RTLD_FINALIZE_FOREIGN_CALL \
428 if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) == 0) \
429 _dl_x86_64_restore_sse (); \
430 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, \
431 old_rtld_must_xmm_save); \
436 #endif /* __ASSEMBLER__ */
437 #endif /* L4 tls disable */