Delete unused TLS_GET_FS, TLS_SET_FS macros.
[glibc.git] / nptl / sysdeps / x86_64 / tls.h
blob454c66a4cec79ab7eb1c537fcaeff4ebab1fe158
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/>. */
19 #ifndef _TLS_H
20 #define _TLS_H 1
22 #ifndef __ASSEMBLER__
23 # include <asm/prctl.h> /* For ARCH_SET_FS. */
24 # include <stdbool.h>
25 # include <stddef.h>
26 # include <stdint.h>
27 # include <stdlib.h>
28 # include <sysdep.h>
29 # include <kernel-features.h>
30 # include <bits/wordsize.h>
31 # include <xmmintrin.h>
34 /* Type for the dtv. */
35 typedef union dtv
37 size_t counter;
38 struct
40 void *val;
41 bool is_static;
42 } pointer;
43 } dtv_t;
46 typedef struct
48 void *tcb; /* Pointer to the TCB. Not necessarily the
49 thread descriptor used by libpthread. */
50 dtv_t *dtv;
51 void *self; /* Pointer to the thread descriptor. */
52 int multiple_threads;
53 int gscope_flag;
54 uintptr_t sysinfo;
55 uintptr_t stack_guard;
56 uintptr_t pointer_guard;
57 unsigned long int vgetcpu_cache[2];
58 # ifndef __ASSUME_PRIVATE_FUTEX
59 int private_futex;
60 # else
61 int __unused1;
62 # endif
63 # if __WORDSIZE == 64
64 int rtld_must_xmm_save;
65 # endif
66 /* Reservation of some values for the TM ABI. */
67 void *__private_tm[5];
68 # if __WORDSIZE == 64
69 long int __unused2;
70 /* Have space for the post-AVX register size. */
71 __m128 rtld_savespace_sse[8][4];
73 void *__padding[8];
74 # endif
75 } tcbhead_t;
77 #else /* __ASSEMBLER__ */
78 # include <tcb-offsets.h>
79 #endif
82 /* Alignment requirement for the stack. */
83 #define STACK_ALIGN 16
86 #ifndef __ASSEMBLER__
87 /* Get system call information. */
88 # include <sysdep.h>
91 /* Get the thread descriptor definition. */
92 # include <nptl/descr.h>
94 #ifndef LOCK_PREFIX
95 # ifdef UP
96 # define LOCK_PREFIX /* nothing */
97 # else
98 # define LOCK_PREFIX "lock;"
99 # endif
100 #endif
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; \
145 int _result; \
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" \
153 : "=a" (_result) \
154 : "0" ((unsigned long int) __NR_arch_prctl), \
155 "D" ((unsigned long int) ARCH_SET_FS), \
156 "S" (_thrdescr) \
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
172 assignments like
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))); \
179 __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" \
190 : "=q" (__value) \
191 : "0" (0), "i" (offsetof (struct pthread, member))); \
192 else if (sizeof (__value) == 4) \
193 asm volatile ("movl %%fs:%P1,%0" \
194 : "=r" (__value) \
195 : "i" (offsetof (struct pthread, member))); \
196 else \
198 if (sizeof (__value) != 8) \
199 /* There should not be any value with a size other than 1, \
200 4 or 8. */ \
201 abort (); \
203 asm volatile ("movq %%fs:%P1,%q0" \
204 : "=r" (__value) \
205 : "i" (offsetof (struct pthread, member))); \
207 __value; })
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" \
215 : "=q" (__value) \
216 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
217 "r" (idx)); \
218 else if (sizeof (__value) == 4) \
219 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
220 : "=r" (__value) \
221 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
222 else \
224 if (sizeof (__value) != 8) \
225 /* There should not be any value with a size other than 1, \
226 4 or 8. */ \
227 abort (); \
229 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
230 : "=r" (__value) \
231 : "i" (offsetof (struct pthread, member[0])), \
232 "r" (idx)); \
234 __value; })
237 /* Loading addresses of objects on x86-64 needs to be treated special
238 when generating PIC code. */
239 #ifdef __pic__
240 # define IMM_MODE "nr"
241 #else
242 # define IMM_MODE "ir"
243 #endif
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" : \
250 : "iq" (value), \
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))); \
256 else \
258 if (sizeof (descr->member) != 8) \
259 /* There should not be any value with a size other than 1, \
260 4 or 8. */ \
261 abort (); \
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)" : \
273 : "iq" (value), \
274 "i" (offsetof (struct pthread, member[0])), \
275 "r" (idx)); \
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])), \
280 "r" (idx)); \
281 else \
283 if (sizeof (descr->member[0]) != 8) \
284 /* There should not be any value with a size other than 1, \
285 4 or 8. */ \
286 abort (); \
288 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
289 : IMM_MODE ((unsigned long int) value), \
290 "i" (offsetof (struct pthread, member[0])), \
291 "r" (idx)); \
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" \
301 : "=a" (__ret) \
302 : "0" (__old), "r" (newval), \
303 "i" (offsetof (struct pthread, member))); \
304 else \
305 /* Not necessary for other sizes in the moment. */ \
306 abort (); \
307 __ret; })
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)), \
315 "ir" (val)); \
316 else \
317 /* Not necessary for other sizes in the moment. */ \
318 abort (); })
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))); \
327 else \
328 /* Not necessary for other sizes in the moment. */ \
329 abort (); })
332 # define CALL_THREAD_FCT(descr) \
333 ({ void *__res; \
334 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
335 "callq *%%fs:%P1" \
336 : "=a" (__res) \
337 : "i" (offsetof (struct pthread, start_routine)), \
338 "i" (offsetof (struct pthread, arg)) \
339 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
340 "memory", "cc"); \
341 __res; })
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() \
365 do \
366 { int __res; \
367 asm volatile ("xchgl %0, %%fs:%P1" \
368 : "=r" (__res) \
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); \
374 while (0)
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) ()
381 # ifdef SHARED
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); \
403 while (0)
405 # define RTLD_FINALIZE_FOREIGN_CALL \
406 do { \
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); \
411 } while (0)
412 # endif
415 #endif /* __ASSEMBLER__ */
417 #endif /* tls.h */