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
24 # include <asm/prctl.h> /* For ARCH_SET_FS. */
30 # include <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 #ifndef HAVE_TLS_SUPPORT
85 # error "TLS support is required."
88 /* Alignment requirement for the stack. */
89 #define STACK_ALIGN 16
93 /* Get system call information. */
97 /* Get the thread descriptor definition. */
98 # include <nptl/descr.h>
102 # define LOCK_PREFIX /* nothing */
104 # define LOCK_PREFIX "lock;"
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)
122 /* The TCB can have any size and the memory following the address the
123 thread pointer points to is unspecified. Allocate the TCB there. */
124 # define TLS_TCB_AT_TP 1
127 /* Install the dtv pointer. The pointer passed is to the element with
128 index -1 which contain the length. */
129 # define INSTALL_DTV(descr, dtvp) \
130 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
132 /* Install new dtv for current thread. */
133 # define INSTALL_NEW_DTV(dtvp) \
134 ({ struct pthread *__pd; \
135 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
137 /* Return dtv of given thread descriptor. */
138 # define GET_DTV(descr) \
139 (((tcbhead_t *) (descr))->dtv)
142 /* Macros to load from and store into segment registers. */
143 # define TLS_GET_FS() \
144 ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
145 # define TLS_SET_FS(val) \
146 __asm ("movl %0, %%fs" :: "q" (val))
149 /* Code to initially initialize the thread pointer. This might need
150 special attention since 'errno' is not yet available and if the
151 operation can cause a failure 'errno' must not be touched.
153 We have to make the syscall for both uses of the macro since the
154 address might be (and probably is) different. */
155 # define TLS_INIT_TP(thrdescr, secondcall) \
156 ({ void *_thrdescr = (thrdescr); \
157 tcbhead_t *_head = _thrdescr; \
160 _head->tcb = _thrdescr; \
161 /* For now the thread descriptor is at the same address. */ \
162 _head->self = _thrdescr; \
164 /* It is a simple syscall to set the %fs value for the thread. */ \
165 asm volatile ("syscall" \
167 : "0" ((unsigned long int) __NR_arch_prctl), \
168 "D" ((unsigned long int) ARCH_SET_FS), \
170 : "memory", "cc", "r11", "cx"); \
172 _result ? "cannot set %fs base address for thread-local storage" : 0; \
176 /* Return the address of the dtv for the current thread. */
177 # define THREAD_DTV() \
178 ({ struct pthread *__pd; \
179 THREAD_GETMEM (__pd, header.dtv); })
182 /* Return the thread descriptor for the current thread.
184 The contained asm must *not* be marked volatile since otherwise
186 pthread_descr self = thread_self();
187 do not get optimized away. */
188 # define THREAD_SELF \
189 ({ struct pthread *__self; \
190 asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
191 : "i" (offsetof (struct pthread, header.self))); \
194 /* Magic for libthread_db to know how to do THREAD_SELF. */
195 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
196 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
198 /* Read member of the thread descriptor directly. */
199 # define THREAD_GETMEM(descr, member) \
200 ({ __typeof (descr->member) __value; \
201 if (sizeof (__value) == 1) \
202 asm volatile ("movb %%fs:%P2,%b0" \
204 : "0" (0), "i" (offsetof (struct pthread, member))); \
205 else if (sizeof (__value) == 4) \
206 asm volatile ("movl %%fs:%P1,%0" \
208 : "i" (offsetof (struct pthread, member))); \
211 if (sizeof (__value) != 8) \
212 /* There should not be any value with a size other than 1, \
216 asm volatile ("movq %%fs:%P1,%q0" \
218 : "i" (offsetof (struct pthread, member))); \
223 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
224 # define THREAD_GETMEM_NC(descr, member, idx) \
225 ({ __typeof (descr->member[0]) __value; \
226 if (sizeof (__value) == 1) \
227 asm volatile ("movb %%fs:%P2(%q3),%b0" \
229 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
231 else if (sizeof (__value) == 4) \
232 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
234 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
237 if (sizeof (__value) != 8) \
238 /* There should not be any value with a size other than 1, \
242 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
244 : "i" (offsetof (struct pthread, member[0])), \
250 /* Loading addresses of objects on x86-64 needs to be treated special
251 when generating PIC code. */
253 # define IMM_MODE "nr"
255 # define IMM_MODE "ir"
259 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
260 # define THREAD_SETMEM(descr, member, value) \
261 ({ if (sizeof (descr->member) == 1) \
262 asm volatile ("movb %b0,%%fs:%P1" : \
264 "i" (offsetof (struct pthread, member))); \
265 else if (sizeof (descr->member) == 4) \
266 asm volatile ("movl %0,%%fs:%P1" : \
267 : IMM_MODE (value), \
268 "i" (offsetof (struct pthread, member))); \
271 if (sizeof (descr->member) != 8) \
272 /* There should not be any value with a size other than 1, \
276 asm volatile ("movq %q0,%%fs:%P1" : \
277 : IMM_MODE ((unsigned long int) value), \
278 "i" (offsetof (struct pthread, member))); \
282 /* Set member of the thread descriptor directly. */
283 # define THREAD_SETMEM_NC(descr, member, idx, value) \
284 ({ if (sizeof (descr->member[0]) == 1) \
285 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
287 "i" (offsetof (struct pthread, member[0])), \
289 else if (sizeof (descr->member[0]) == 4) \
290 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
291 : IMM_MODE (value), \
292 "i" (offsetof (struct pthread, member[0])), \
296 if (sizeof (descr->member[0]) != 8) \
297 /* There should not be any value with a size other than 1, \
301 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
302 : IMM_MODE ((unsigned long int) value), \
303 "i" (offsetof (struct pthread, member[0])), \
308 /* Atomic compare and exchange on TLS, returning old value. */
309 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
310 ({ __typeof (descr->member) __ret; \
311 __typeof (oldval) __old = (oldval); \
312 if (sizeof (descr->member) == 4) \
313 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
315 : "0" (__old), "r" (newval), \
316 "i" (offsetof (struct pthread, member))); \
318 /* Not necessary for other sizes in the moment. */ \
323 /* Atomic logical and. */
324 # define THREAD_ATOMIC_AND(descr, member, val) \
325 (void) ({ if (sizeof ((descr)->member) == 4) \
326 asm volatile (LOCK_PREFIX "andl %1, %%fs:%P0" \
327 :: "i" (offsetof (struct pthread, member)), \
330 /* Not necessary for other sizes in the moment. */ \
334 /* Atomic set bit. */
335 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
336 (void) ({ if (sizeof ((descr)->member) == 4) \
337 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
338 :: "i" (offsetof (struct pthread, member)), \
339 "ir" (1 << (bit))); \
341 /* Not necessary for other sizes in the moment. */ \
345 # define CALL_THREAD_FCT(descr) \
347 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
350 : "i" (offsetof (struct pthread, start_routine)), \
351 "i" (offsetof (struct pthread, arg)) \
352 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
357 /* Set the stack guard field in TCB head. */
358 # define THREAD_SET_STACK_GUARD(value) \
359 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
360 # define THREAD_COPY_STACK_GUARD(descr) \
361 ((descr)->header.stack_guard \
362 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
365 /* Set the pointer guard field in the TCB head. */
366 # define THREAD_SET_POINTER_GUARD(value) \
367 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
368 # define THREAD_COPY_POINTER_GUARD(descr) \
369 ((descr)->header.pointer_guard \
370 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
373 /* Get and set the global scope generation counter in the TCB head. */
374 # define THREAD_GSCOPE_FLAG_UNUSED 0
375 # define THREAD_GSCOPE_FLAG_USED 1
376 # define THREAD_GSCOPE_FLAG_WAIT 2
377 # define THREAD_GSCOPE_RESET_FLAG() \
380 asm volatile ("xchgl %0, %%fs:%P1" \
382 : "i" (offsetof (struct pthread, header.gscope_flag)), \
383 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
384 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
385 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
388 # define THREAD_GSCOPE_SET_FLAG() \
389 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
390 # define THREAD_GSCOPE_WAIT() \
391 GL(dl_wait_lookup_done) ()
395 /* Defined in dl-trampoline.S. */
396 extern void _dl_x86_64_save_sse (void);
397 extern void _dl_x86_64_restore_sse (void);
399 # define RTLD_CHECK_FOREIGN_CALL \
400 (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
402 /* NB: Don't use the xchg operation because that would imply a lock
403 prefix which is expensive and unnecessary. The cache line is also
404 not contested at all. */
405 # define RTLD_ENABLE_FOREIGN_CALL \
406 int old_rtld_must_xmm_save = THREAD_GETMEM (THREAD_SELF, \
407 header.rtld_must_xmm_save); \
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); \
418 # define RTLD_FINALIZE_FOREIGN_CALL \
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, \
423 old_rtld_must_xmm_save); \
428 #endif /* __ASSEMBLER__ */