1 /* Definition for thread-local data handling. nptl/x86_64 version.
2 Copyright (C) 2002-2015 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 <libc-internal.h>
30 # include <kernel-features.h>
32 /* Replacement type for __m128 since this file is included by ld.so,
33 which is compiled with -mno-sse. It must not change the alignment
34 of rtld_savespace_sse. */
41 /* Type for the dtv. */
55 void *tcb
; /* Pointer to the TCB. Not necessarily the
56 thread descriptor used by libpthread. */
58 void *self
; /* Pointer to the thread descriptor. */
62 uintptr_t stack_guard
;
63 uintptr_t pointer_guard
;
64 unsigned long int vgetcpu_cache
[2];
65 # ifndef __ASSUME_PRIVATE_FUTEX
68 int __glibc_reserved1
;
70 int rtld_must_xmm_save
;
71 /* Reservation of some values for the TM ABI. */
72 void *__private_tm
[4];
73 /* GCC split stack support. */
75 long int __glibc_reserved2
;
76 /* Have space for the post-AVX register size. */
77 __128bits rtld_savespace_sse
[8][4] __attribute__ ((aligned (32)));
82 #else /* __ASSEMBLER__ */
83 # include <tcb-offsets.h>
87 /* Alignment requirement for the stack. */
88 #define STACK_ALIGN 16
92 /* Get system call information. */
97 # define LOCK_PREFIX /* nothing */
99 # define LOCK_PREFIX "lock;"
103 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
104 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
105 struct pthread even when not linked with -lpthread. */
106 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
108 /* Alignment requirements for the initial TCB. */
109 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
111 /* This is the size of the TCB. */
112 # define TLS_TCB_SIZE sizeof (struct pthread)
114 /* Alignment requirements for the TCB. */
115 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
117 /* The TCB can have any size and the memory following the address the
118 thread pointer points to is unspecified. Allocate the TCB there. */
119 # define TLS_TCB_AT_TP 1
120 # define TLS_DTV_AT_TP 0
122 /* Get the thread descriptor definition. */
123 # include <nptl/descr.h>
126 /* Install the dtv pointer. The pointer passed is to the element with
127 index -1 which contain the length. */
128 # define INSTALL_DTV(descr, dtvp) \
129 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
131 /* Install new dtv for current thread. */
132 # define INSTALL_NEW_DTV(dtvp) \
133 ({ struct pthread *__pd; \
134 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
136 /* Return dtv of given thread descriptor. */
137 # define GET_DTV(descr) \
138 (((tcbhead_t *) (descr))->dtv)
141 /* Code to initially initialize the thread pointer. This might need
142 special attention since 'errno' is not yet available and if the
143 operation can cause a failure 'errno' must not be touched.
145 We have to make the syscall for both uses of the macro since the
146 address might be (and probably is) different. */
147 # define TLS_INIT_TP(thrdescr) \
148 ({ void *_thrdescr = (thrdescr); \
149 tcbhead_t *_head = _thrdescr; \
152 _head->tcb = _thrdescr; \
153 /* For now the thread descriptor is at the same address. */ \
154 _head->self = _thrdescr; \
156 /* It is a simple syscall to set the %fs value for the thread. */ \
157 asm volatile ("syscall" \
159 : "0" ((unsigned long int) __NR_arch_prctl), \
160 "D" ((unsigned long int) ARCH_SET_FS), \
162 : "memory", "cc", "r11", "cx"); \
164 _result ? "cannot set %fs base address for thread-local storage" : 0; \
167 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd)
170 /* Return the address of the dtv for the current thread. */
171 # define THREAD_DTV() \
172 ({ struct pthread *__pd; \
173 THREAD_GETMEM (__pd, header.dtv); })
176 /* Return the thread descriptor for the current thread.
178 The contained asm must *not* be marked volatile since otherwise
180 pthread_descr self = thread_self();
181 do not get optimized away. */
182 # define THREAD_SELF \
183 ({ struct pthread *__self; \
184 asm ("mov %%fs:%c1,%0" : "=r" (__self) \
185 : "i" (offsetof (struct pthread, header.self))); \
188 /* Magic for libthread_db to know how to do THREAD_SELF. */
189 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
190 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
192 /* Read member of the thread descriptor directly. */
193 # define THREAD_GETMEM(descr, member) \
194 ({ __typeof (descr->member) __value; \
195 if (sizeof (__value) == 1) \
196 asm volatile ("movb %%fs:%P2,%b0" \
198 : "0" (0), "i" (offsetof (struct pthread, member))); \
199 else if (sizeof (__value) == 4) \
200 asm volatile ("movl %%fs:%P1,%0" \
202 : "i" (offsetof (struct pthread, member))); \
205 if (sizeof (__value) != 8) \
206 /* There should not be any value with a size other than 1, \
210 asm volatile ("movq %%fs:%P1,%q0" \
212 : "i" (offsetof (struct pthread, member))); \
217 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
218 # define THREAD_GETMEM_NC(descr, member, idx) \
219 ({ __typeof (descr->member[0]) __value; \
220 if (sizeof (__value) == 1) \
221 asm volatile ("movb %%fs:%P2(%q3),%b0" \
223 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
225 else if (sizeof (__value) == 4) \
226 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
228 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
231 if (sizeof (__value) != 8) \
232 /* There should not be any value with a size other than 1, \
236 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
238 : "i" (offsetof (struct pthread, member[0])), \
244 /* Loading addresses of objects on x86-64 needs to be treated special
245 when generating PIC code. */
247 # define IMM_MODE "nr"
249 # define IMM_MODE "ir"
253 /* Set member of the thread descriptor directly. */
254 # define THREAD_SETMEM(descr, member, value) \
255 ({ if (sizeof (descr->member) == 1) \
256 asm volatile ("movb %b0,%%fs:%P1" : \
258 "i" (offsetof (struct pthread, member))); \
259 else if (sizeof (descr->member) == 4) \
260 asm volatile ("movl %0,%%fs:%P1" : \
261 : IMM_MODE (value), \
262 "i" (offsetof (struct pthread, member))); \
265 if (sizeof (descr->member) != 8) \
266 /* There should not be any value with a size other than 1, \
270 asm volatile ("movq %q0,%%fs:%P1" : \
271 : IMM_MODE ((uint64_t) cast_to_integer (value)), \
272 "i" (offsetof (struct pthread, member))); \
276 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
277 # define THREAD_SETMEM_NC(descr, member, idx, value) \
278 ({ if (sizeof (descr->member[0]) == 1) \
279 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
281 "i" (offsetof (struct pthread, member[0])), \
283 else if (sizeof (descr->member[0]) == 4) \
284 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
285 : IMM_MODE (value), \
286 "i" (offsetof (struct pthread, member[0])), \
290 if (sizeof (descr->member[0]) != 8) \
291 /* There should not be any value with a size other than 1, \
295 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
296 : IMM_MODE ((uint64_t) cast_to_integer (value)), \
297 "i" (offsetof (struct pthread, member[0])), \
302 /* Atomic compare and exchange on TLS, returning old value. */
303 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
304 ({ __typeof (descr->member) __ret; \
305 __typeof (oldval) __old = (oldval); \
306 if (sizeof (descr->member) == 4) \
307 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
309 : "0" (__old), "r" (newval), \
310 "i" (offsetof (struct pthread, member))); \
312 /* Not necessary for other sizes in the moment. */ \
317 /* Atomic logical and. */
318 # define THREAD_ATOMIC_AND(descr, member, val) \
319 (void) ({ if (sizeof ((descr)->member) == 4) \
320 asm volatile (LOCK_PREFIX "andl %1, %%fs:%P0" \
321 :: "i" (offsetof (struct pthread, member)), \
324 /* Not necessary for other sizes in the moment. */ \
328 /* Atomic set bit. */
329 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
330 (void) ({ if (sizeof ((descr)->member) == 4) \
331 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
332 :: "i" (offsetof (struct pthread, member)), \
333 "ir" (1 << (bit))); \
335 /* Not necessary for other sizes in the moment. */ \
339 # define CALL_THREAD_FCT(descr) \
341 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
344 : "i" (offsetof (struct pthread, start_routine)), \
345 "i" (offsetof (struct pthread, arg)) \
346 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
351 /* Set the stack guard field in TCB head. */
352 # define THREAD_SET_STACK_GUARD(value) \
353 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
354 # define THREAD_COPY_STACK_GUARD(descr) \
355 ((descr)->header.stack_guard \
356 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
359 /* Set the pointer guard field in the TCB head. */
360 # define THREAD_SET_POINTER_GUARD(value) \
361 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
362 # define THREAD_COPY_POINTER_GUARD(descr) \
363 ((descr)->header.pointer_guard \
364 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
367 /* Get and set the global scope generation counter in the TCB head. */
368 # define THREAD_GSCOPE_FLAG_UNUSED 0
369 # define THREAD_GSCOPE_FLAG_USED 1
370 # define THREAD_GSCOPE_FLAG_WAIT 2
371 # define THREAD_GSCOPE_RESET_FLAG() \
374 asm volatile ("xchgl %0, %%fs:%P1" \
376 : "i" (offsetof (struct pthread, header.gscope_flag)), \
377 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
378 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
379 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
382 # define THREAD_GSCOPE_SET_FLAG() \
383 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
384 # define THREAD_GSCOPE_WAIT() \
385 GL(dl_wait_lookup_done) ()
389 /* Defined in dl-trampoline.S. */
390 extern void _dl_x86_64_save_sse (void);
391 extern void _dl_x86_64_restore_sse (void);
393 # define RTLD_CHECK_FOREIGN_CALL \
394 (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
396 /* NB: Don't use the xchg operation because that would imply a lock
397 prefix which is expensive and unnecessary. The cache line is also
398 not contested at all. */
399 # define RTLD_ENABLE_FOREIGN_CALL \
400 int old_rtld_must_xmm_save = THREAD_GETMEM (THREAD_SELF, \
401 header.rtld_must_xmm_save); \
402 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 1)
404 # define RTLD_PREPARE_FOREIGN_CALL \
405 do if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save)) \
407 _dl_x86_64_save_sse (); \
408 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0); \
412 # define RTLD_FINALIZE_FOREIGN_CALL \
414 if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) == 0) \
415 _dl_x86_64_restore_sse (); \
416 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, \
417 old_rtld_must_xmm_save); \
422 #endif /* __ASSEMBLER__ */