1 /* Definition for thread-local data handling. nptl/x86_64 version.
2 Copyright (C) 2002-2009, 2011-2012 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/>. */
23 # include <asm/prctl.h> /* For ARCH_SET_FS. */
29 # include <kernel-features.h>
30 # include <bits/wordsize.h>
31 # include <xmmintrin.h>
34 /* Type for the dtv. */
48 void *tcb
; /* Pointer to the TCB. Not necessarily the
49 thread descriptor used by libpthread. */
51 void *self
; /* Pointer to the thread descriptor. */
55 uintptr_t stack_guard
;
56 uintptr_t pointer_guard
;
57 unsigned long int vgetcpu_cache
[2];
58 # ifndef __ASSUME_PRIVATE_FUTEX
64 int rtld_must_xmm_save
;
66 /* Reservation of some values for the TM ABI. */
67 void *__private_tm
[5];
70 /* Have space for the post-AVX register size. */
71 __m128 rtld_savespace_sse
[8][4];
77 #else /* __ASSEMBLER__ */
78 # include <tcb-offsets.h>
82 /* Alignment requirement for the stack. */
83 #define STACK_ALIGN 16
87 /* Get system call information. */
91 /* Get the thread descriptor definition. */
92 # include <nptl/descr.h>
96 # define LOCK_PREFIX /* nothing */
98 # define LOCK_PREFIX "lock;"
102 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
103 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
104 struct pthread even when not linked with -lpthread. */
105 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
107 /* Alignment requirements for the initial TCB. */
108 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
110 /* This is the size of the TCB. */
111 # define TLS_TCB_SIZE sizeof (struct pthread)
113 /* Alignment requirements for the TCB. */
114 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
116 /* The TCB can have any size and the memory following the address the
117 thread pointer points to is unspecified. Allocate the TCB there. */
118 # define TLS_TCB_AT_TP 1
121 /* Install the dtv pointer. The pointer passed is to the element with
122 index -1 which contain the length. */
123 # define INSTALL_DTV(descr, dtvp) \
124 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
126 /* Install new dtv for current thread. */
127 # define INSTALL_NEW_DTV(dtvp) \
128 ({ struct pthread *__pd; \
129 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
131 /* Return dtv of given thread descriptor. */
132 # define GET_DTV(descr) \
133 (((tcbhead_t *) (descr))->dtv)
136 /* Code to initially initialize the thread pointer. This might need
137 special attention since 'errno' is not yet available and if the
138 operation can cause a failure 'errno' must not be touched.
140 We have to make the syscall for both uses of the macro since the
141 address might be (and probably is) different. */
142 # define TLS_INIT_TP(thrdescr, secondcall) \
143 ({ void *_thrdescr = (thrdescr); \
144 tcbhead_t *_head = _thrdescr; \
147 _head->tcb = _thrdescr; \
148 /* For now the thread descriptor is at the same address. */ \
149 _head->self = _thrdescr; \
151 /* It is a simple syscall to set the %fs value for the thread. */ \
152 asm volatile ("syscall" \
154 : "0" ((unsigned long int) __NR_arch_prctl), \
155 "D" ((unsigned long int) ARCH_SET_FS), \
157 : "memory", "cc", "r11", "cx"); \
159 _result ? "cannot set %fs base address for thread-local storage" : 0; \
163 /* Return the address of the dtv for the current thread. */
164 # define THREAD_DTV() \
165 ({ struct pthread *__pd; \
166 THREAD_GETMEM (__pd, header.dtv); })
169 /* Return the thread descriptor for the current thread.
171 The contained asm must *not* be marked volatile since otherwise
173 pthread_descr self = thread_self();
174 do not get optimized away. */
175 # define THREAD_SELF \
176 ({ struct pthread *__self; \
177 asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
178 : "i" (offsetof (struct pthread, header.self))); \
181 /* Magic for libthread_db to know how to do THREAD_SELF. */
182 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
183 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
185 /* Read member of the thread descriptor directly. */
186 # define THREAD_GETMEM(descr, member) \
187 ({ __typeof (descr->member) __value; \
188 if (sizeof (__value) == 1) \
189 asm volatile ("movb %%fs:%P2,%b0" \
191 : "0" (0), "i" (offsetof (struct pthread, member))); \
192 else if (sizeof (__value) == 4) \
193 asm volatile ("movl %%fs:%P1,%0" \
195 : "i" (offsetof (struct pthread, member))); \
198 if (sizeof (__value) != 8) \
199 /* There should not be any value with a size other than 1, \
203 asm volatile ("movq %%fs:%P1,%q0" \
205 : "i" (offsetof (struct pthread, member))); \
210 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
211 # define THREAD_GETMEM_NC(descr, member, idx) \
212 ({ __typeof (descr->member[0]) __value; \
213 if (sizeof (__value) == 1) \
214 asm volatile ("movb %%fs:%P2(%q3),%b0" \
216 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
218 else if (sizeof (__value) == 4) \
219 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
221 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
224 if (sizeof (__value) != 8) \
225 /* There should not be any value with a size other than 1, \
229 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
231 : "i" (offsetof (struct pthread, member[0])), \
237 /* Loading addresses of objects on x86-64 needs to be treated special
238 when generating PIC code. */
240 # define IMM_MODE "nr"
242 # define IMM_MODE "ir"
246 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
247 # define THREAD_SETMEM(descr, member, value) \
248 ({ if (sizeof (descr->member) == 1) \
249 asm volatile ("movb %b0,%%fs:%P1" : \
251 "i" (offsetof (struct pthread, member))); \
252 else if (sizeof (descr->member) == 4) \
253 asm volatile ("movl %0,%%fs:%P1" : \
254 : IMM_MODE (value), \
255 "i" (offsetof (struct pthread, member))); \
258 if (sizeof (descr->member) != 8) \
259 /* There should not be any value with a size other than 1, \
263 asm volatile ("movq %q0,%%fs:%P1" : \
264 : IMM_MODE ((unsigned long int) value), \
265 "i" (offsetof (struct pthread, member))); \
269 /* Set member of the thread descriptor directly. */
270 # define THREAD_SETMEM_NC(descr, member, idx, value) \
271 ({ if (sizeof (descr->member[0]) == 1) \
272 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
274 "i" (offsetof (struct pthread, member[0])), \
276 else if (sizeof (descr->member[0]) == 4) \
277 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
278 : IMM_MODE (value), \
279 "i" (offsetof (struct pthread, member[0])), \
283 if (sizeof (descr->member[0]) != 8) \
284 /* There should not be any value with a size other than 1, \
288 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
289 : IMM_MODE ((unsigned long int) value), \
290 "i" (offsetof (struct pthread, member[0])), \
295 /* Atomic compare and exchange on TLS, returning old value. */
296 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
297 ({ __typeof (descr->member) __ret; \
298 __typeof (oldval) __old = (oldval); \
299 if (sizeof (descr->member) == 4) \
300 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
302 : "0" (__old), "r" (newval), \
303 "i" (offsetof (struct pthread, member))); \
305 /* Not necessary for other sizes in the moment. */ \
310 /* Atomic logical and. */
311 # define THREAD_ATOMIC_AND(descr, member, val) \
312 (void) ({ if (sizeof ((descr)->member) == 4) \
313 asm volatile (LOCK_PREFIX "andl %1, %%fs:%P0" \
314 :: "i" (offsetof (struct pthread, member)), \
317 /* Not necessary for other sizes in the moment. */ \
321 /* Atomic set bit. */
322 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
323 (void) ({ if (sizeof ((descr)->member) == 4) \
324 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
325 :: "i" (offsetof (struct pthread, member)), \
326 "ir" (1 << (bit))); \
328 /* Not necessary for other sizes in the moment. */ \
332 # define CALL_THREAD_FCT(descr) \
334 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
337 : "i" (offsetof (struct pthread, start_routine)), \
338 "i" (offsetof (struct pthread, arg)) \
339 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
344 /* Set the stack guard field in TCB head. */
345 # define THREAD_SET_STACK_GUARD(value) \
346 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
347 # define THREAD_COPY_STACK_GUARD(descr) \
348 ((descr)->header.stack_guard \
349 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
352 /* Set the pointer guard field in the TCB head. */
353 # define THREAD_SET_POINTER_GUARD(value) \
354 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
355 # define THREAD_COPY_POINTER_GUARD(descr) \
356 ((descr)->header.pointer_guard \
357 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
360 /* Get and set the global scope generation counter in the TCB head. */
361 # define THREAD_GSCOPE_FLAG_UNUSED 0
362 # define THREAD_GSCOPE_FLAG_USED 1
363 # define THREAD_GSCOPE_FLAG_WAIT 2
364 # define THREAD_GSCOPE_RESET_FLAG() \
367 asm volatile ("xchgl %0, %%fs:%P1" \
369 : "i" (offsetof (struct pthread, header.gscope_flag)), \
370 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
371 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
372 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
375 # define THREAD_GSCOPE_SET_FLAG() \
376 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
377 # define THREAD_GSCOPE_WAIT() \
378 GL(dl_wait_lookup_done) ()
382 /* Defined in dl-trampoline.S. */
383 extern void _dl_x86_64_save_sse (void);
384 extern void _dl_x86_64_restore_sse (void);
386 # define RTLD_CHECK_FOREIGN_CALL \
387 (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
389 /* NB: Don't use the xchg operation because that would imply a lock
390 prefix which is expensive and unnecessary. The cache line is also
391 not contested at all. */
392 # define RTLD_ENABLE_FOREIGN_CALL \
393 int old_rtld_must_xmm_save = THREAD_GETMEM (THREAD_SELF, \
394 header.rtld_must_xmm_save); \
395 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 1)
397 # define RTLD_PREPARE_FOREIGN_CALL \
398 do if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save)) \
400 _dl_x86_64_save_sse (); \
401 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0); \
405 # define RTLD_FINALIZE_FOREIGN_CALL \
407 if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) == 0) \
408 _dl_x86_64_restore_sse (); \
409 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, \
410 old_rtld_must_xmm_save); \
415 #endif /* __ASSEMBLER__ */