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 <xmmintrin.h>
33 /* Type for the dtv. */
47 void *tcb
; /* Pointer to the TCB. Not necessarily the
48 thread descriptor used by libpthread. */
50 void *self
; /* Pointer to the thread descriptor. */
54 uintptr_t stack_guard
;
55 uintptr_t pointer_guard
;
56 unsigned long int vgetcpu_cache
[2];
57 # ifndef __ASSUME_PRIVATE_FUTEX
62 int rtld_must_xmm_save
;
63 /* Reservation of some values for the TM ABI. */
64 void *__private_tm
[5];
66 /* Have space for the post-AVX register size. */
67 __m128 rtld_savespace_sse
[8][4] __attribute__ ((aligned (32)));
72 #else /* __ASSEMBLER__ */
73 # include <tcb-offsets.h>
77 /* Alignment requirement for the stack. */
78 #define STACK_ALIGN 16
82 /* Get system call information. */
86 /* Get the thread descriptor definition. */
87 # include <nptl/descr.h>
91 # define LOCK_PREFIX /* nothing */
93 # define LOCK_PREFIX "lock;"
97 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
98 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
99 struct pthread even when not linked with -lpthread. */
100 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
102 /* Alignment requirements for the initial TCB. */
103 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
105 /* This is the size of the TCB. */
106 # define TLS_TCB_SIZE sizeof (struct pthread)
108 /* Alignment requirements for the TCB. */
109 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
111 /* The TCB can have any size and the memory following the address the
112 thread pointer points to is unspecified. Allocate the TCB there. */
113 # define TLS_TCB_AT_TP 1
116 /* Install the dtv pointer. The pointer passed is to the element with
117 index -1 which contain the length. */
118 # define INSTALL_DTV(descr, dtvp) \
119 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
121 /* Install new dtv for current thread. */
122 # define INSTALL_NEW_DTV(dtvp) \
123 ({ struct pthread *__pd; \
124 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
126 /* Return dtv of given thread descriptor. */
127 # define GET_DTV(descr) \
128 (((tcbhead_t *) (descr))->dtv)
131 /* Code to initially initialize the thread pointer. This might need
132 special attention since 'errno' is not yet available and if the
133 operation can cause a failure 'errno' must not be touched.
135 We have to make the syscall for both uses of the macro since the
136 address might be (and probably is) different. */
137 # define TLS_INIT_TP(thrdescr, secondcall) \
138 ({ void *_thrdescr = (thrdescr); \
139 tcbhead_t *_head = _thrdescr; \
142 _head->tcb = _thrdescr; \
143 /* For now the thread descriptor is at the same address. */ \
144 _head->self = _thrdescr; \
146 /* It is a simple syscall to set the %fs value for the thread. */ \
147 asm volatile ("syscall" \
149 : "0" ((unsigned long int) __NR_arch_prctl), \
150 "D" ((unsigned long int) ARCH_SET_FS), \
152 : "memory", "cc", "r11", "cx"); \
154 _result ? "cannot set %fs base address for thread-local storage" : 0; \
158 /* Return the address of the dtv for the current thread. */
159 # define THREAD_DTV() \
160 ({ struct pthread *__pd; \
161 THREAD_GETMEM (__pd, header.dtv); })
164 /* Return the thread descriptor for the current thread.
166 The contained asm must *not* be marked volatile since otherwise
168 pthread_descr self = thread_self();
169 do not get optimized away. */
170 # define THREAD_SELF \
171 ({ struct pthread *__self; \
172 asm ("mov %%fs:%c1,%0" : "=r" (__self) \
173 : "i" (offsetof (struct pthread, header.self))); \
176 /* Magic for libthread_db to know how to do THREAD_SELF. */
177 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
178 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
180 /* Read member of the thread descriptor directly. */
181 # define THREAD_GETMEM(descr, member) \
182 ({ __typeof (descr->member) __value; \
183 if (sizeof (__value) == 1) \
184 asm volatile ("movb %%fs:%P2,%b0" \
186 : "0" (0), "i" (offsetof (struct pthread, member))); \
187 else if (sizeof (__value) == 4) \
188 asm volatile ("movl %%fs:%P1,%0" \
190 : "i" (offsetof (struct pthread, member))); \
193 if (sizeof (__value) != 8) \
194 /* There should not be any value with a size other than 1, \
198 asm volatile ("movq %%fs:%P1,%q0" \
200 : "i" (offsetof (struct pthread, member))); \
205 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
206 # define THREAD_GETMEM_NC(descr, member, idx) \
207 ({ __typeof (descr->member[0]) __value; \
208 if (sizeof (__value) == 1) \
209 asm volatile ("movb %%fs:%P2(%q3),%b0" \
211 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
213 else if (sizeof (__value) == 4) \
214 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
216 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
219 if (sizeof (__value) != 8) \
220 /* There should not be any value with a size other than 1, \
224 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
226 : "i" (offsetof (struct pthread, member[0])), \
232 /* Loading addresses of objects on x86-64 needs to be treated special
233 when generating PIC code. */
235 # define IMM_MODE "nr"
237 # define IMM_MODE "ir"
241 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
242 # define THREAD_SETMEM(descr, member, value) \
243 ({ if (sizeof (descr->member) == 1) \
244 asm volatile ("movb %b0,%%fs:%P1" : \
246 "i" (offsetof (struct pthread, member))); \
247 else if (sizeof (descr->member) == 4) \
248 asm volatile ("movl %0,%%fs:%P1" : \
249 : IMM_MODE (value), \
250 "i" (offsetof (struct pthread, member))); \
253 if (sizeof (descr->member) != 8) \
254 /* There should not be any value with a size other than 1, \
258 asm volatile ("movq %q0,%%fs:%P1" : \
259 : IMM_MODE ((uint64_t) (value)), \
260 "i" (offsetof (struct pthread, member))); \
264 /* Set member of the thread descriptor directly. */
265 # define THREAD_SETMEM_NC(descr, member, idx, value) \
266 ({ if (sizeof (descr->member[0]) == 1) \
267 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
269 "i" (offsetof (struct pthread, member[0])), \
271 else if (sizeof (descr->member[0]) == 4) \
272 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
273 : IMM_MODE (value), \
274 "i" (offsetof (struct pthread, member[0])), \
278 if (sizeof (descr->member[0]) != 8) \
279 /* There should not be any value with a size other than 1, \
283 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
284 : IMM_MODE ((uint64_t) (value)), \
285 "i" (offsetof (struct pthread, member[0])), \
290 /* Atomic compare and exchange on TLS, returning old value. */
291 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
292 ({ __typeof (descr->member) __ret; \
293 __typeof (oldval) __old = (oldval); \
294 if (sizeof (descr->member) == 4) \
295 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
297 : "0" (__old), "r" (newval), \
298 "i" (offsetof (struct pthread, member))); \
300 /* Not necessary for other sizes in the moment. */ \
305 /* Atomic logical and. */
306 # define THREAD_ATOMIC_AND(descr, member, val) \
307 (void) ({ if (sizeof ((descr)->member) == 4) \
308 asm volatile (LOCK_PREFIX "andl %1, %%fs:%P0" \
309 :: "i" (offsetof (struct pthread, member)), \
312 /* Not necessary for other sizes in the moment. */ \
316 /* Atomic set bit. */
317 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
318 (void) ({ if (sizeof ((descr)->member) == 4) \
319 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
320 :: "i" (offsetof (struct pthread, member)), \
321 "ir" (1 << (bit))); \
323 /* Not necessary for other sizes in the moment. */ \
327 # define CALL_THREAD_FCT(descr) \
329 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
332 : "i" (offsetof (struct pthread, start_routine)), \
333 "i" (offsetof (struct pthread, arg)) \
334 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
339 /* Set the stack guard field in TCB head. */
340 # define THREAD_SET_STACK_GUARD(value) \
341 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
342 # define THREAD_COPY_STACK_GUARD(descr) \
343 ((descr)->header.stack_guard \
344 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
347 /* Set the pointer guard field in the TCB head. */
348 # define THREAD_SET_POINTER_GUARD(value) \
349 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
350 # define THREAD_COPY_POINTER_GUARD(descr) \
351 ((descr)->header.pointer_guard \
352 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
355 /* Get and set the global scope generation counter in the TCB head. */
356 # define THREAD_GSCOPE_FLAG_UNUSED 0
357 # define THREAD_GSCOPE_FLAG_USED 1
358 # define THREAD_GSCOPE_FLAG_WAIT 2
359 # define THREAD_GSCOPE_RESET_FLAG() \
362 asm volatile ("xchgl %0, %%fs:%P1" \
364 : "i" (offsetof (struct pthread, header.gscope_flag)), \
365 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
366 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
367 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
370 # define THREAD_GSCOPE_SET_FLAG() \
371 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
372 # define THREAD_GSCOPE_WAIT() \
373 GL(dl_wait_lookup_done) ()
377 /* Defined in dl-trampoline.S. */
378 extern void _dl_x86_64_save_sse (void);
379 extern void _dl_x86_64_restore_sse (void);
381 # define RTLD_CHECK_FOREIGN_CALL \
382 (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
384 /* NB: Don't use the xchg operation because that would imply a lock
385 prefix which is expensive and unnecessary. The cache line is also
386 not contested at all. */
387 # define RTLD_ENABLE_FOREIGN_CALL \
388 int old_rtld_must_xmm_save = THREAD_GETMEM (THREAD_SELF, \
389 header.rtld_must_xmm_save); \
390 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 1)
392 # define RTLD_PREPARE_FOREIGN_CALL \
393 do if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save)) \
395 _dl_x86_64_save_sse (); \
396 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0); \
400 # define RTLD_FINALIZE_FOREIGN_CALL \
402 if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) == 0) \
403 _dl_x86_64_restore_sse (); \
404 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, \
405 old_rtld_must_xmm_save); \
410 #endif /* __ASSEMBLER__ */