Add "()" when casting to uint64_t for 64-bit store
[glibc.git] / nptl / sysdeps / x86_64 / tls.h
blobb651d1cfa8840cfa4f27aa197c82e8f01625b191
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 <xmmintrin.h>
33 /* Type for the dtv. */
34 typedef union dtv
36 size_t counter;
37 struct
39 void *val;
40 bool is_static;
41 } pointer;
42 } dtv_t;
45 typedef struct
47 void *tcb; /* Pointer to the TCB. Not necessarily the
48 thread descriptor used by libpthread. */
49 dtv_t *dtv;
50 void *self; /* Pointer to the thread descriptor. */
51 int multiple_threads;
52 int gscope_flag;
53 uintptr_t sysinfo;
54 uintptr_t stack_guard;
55 uintptr_t pointer_guard;
56 unsigned long int vgetcpu_cache[2];
57 # ifndef __ASSUME_PRIVATE_FUTEX
58 int private_futex;
59 # else
60 int __unused1;
61 # endif
62 int rtld_must_xmm_save;
63 /* Reservation of some values for the TM ABI. */
64 void *__private_tm[5];
65 long int __unused2;
66 /* Have space for the post-AVX register size. */
67 __m128 rtld_savespace_sse[8][4] __attribute__ ((aligned (32)));
69 void *__padding[8];
70 } tcbhead_t;
72 #else /* __ASSEMBLER__ */
73 # include <tcb-offsets.h>
74 #endif
77 /* Alignment requirement for the stack. */
78 #define STACK_ALIGN 16
81 #ifndef __ASSEMBLER__
82 /* Get system call information. */
83 # include <sysdep.h>
86 /* Get the thread descriptor definition. */
87 # include <nptl/descr.h>
89 #ifndef LOCK_PREFIX
90 # ifdef UP
91 # define LOCK_PREFIX /* nothing */
92 # else
93 # define LOCK_PREFIX "lock;"
94 # endif
95 #endif
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; \
140 int _result; \
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" \
148 : "=a" (_result) \
149 : "0" ((unsigned long int) __NR_arch_prctl), \
150 "D" ((unsigned long int) ARCH_SET_FS), \
151 "S" (_thrdescr) \
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
167 assignments like
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))); \
174 __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" \
185 : "=q" (__value) \
186 : "0" (0), "i" (offsetof (struct pthread, member))); \
187 else if (sizeof (__value) == 4) \
188 asm volatile ("movl %%fs:%P1,%0" \
189 : "=r" (__value) \
190 : "i" (offsetof (struct pthread, member))); \
191 else \
193 if (sizeof (__value) != 8) \
194 /* There should not be any value with a size other than 1, \
195 4 or 8. */ \
196 abort (); \
198 asm volatile ("movq %%fs:%P1,%q0" \
199 : "=r" (__value) \
200 : "i" (offsetof (struct pthread, member))); \
202 __value; })
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" \
210 : "=q" (__value) \
211 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
212 "r" (idx)); \
213 else if (sizeof (__value) == 4) \
214 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
215 : "=r" (__value) \
216 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
217 else \
219 if (sizeof (__value) != 8) \
220 /* There should not be any value with a size other than 1, \
221 4 or 8. */ \
222 abort (); \
224 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
225 : "=r" (__value) \
226 : "i" (offsetof (struct pthread, member[0])), \
227 "r" (idx)); \
229 __value; })
232 /* Loading addresses of objects on x86-64 needs to be treated special
233 when generating PIC code. */
234 #ifdef __pic__
235 # define IMM_MODE "nr"
236 #else
237 # define IMM_MODE "ir"
238 #endif
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" : \
245 : "iq" (value), \
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))); \
251 else \
253 if (sizeof (descr->member) != 8) \
254 /* There should not be any value with a size other than 1, \
255 4 or 8. */ \
256 abort (); \
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)" : \
268 : "iq" (value), \
269 "i" (offsetof (struct pthread, member[0])), \
270 "r" (idx)); \
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])), \
275 "r" (idx)); \
276 else \
278 if (sizeof (descr->member[0]) != 8) \
279 /* There should not be any value with a size other than 1, \
280 4 or 8. */ \
281 abort (); \
283 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
284 : IMM_MODE ((uint64_t) (value)), \
285 "i" (offsetof (struct pthread, member[0])), \
286 "r" (idx)); \
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" \
296 : "=a" (__ret) \
297 : "0" (__old), "r" (newval), \
298 "i" (offsetof (struct pthread, member))); \
299 else \
300 /* Not necessary for other sizes in the moment. */ \
301 abort (); \
302 __ret; })
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)), \
310 "ir" (val)); \
311 else \
312 /* Not necessary for other sizes in the moment. */ \
313 abort (); })
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))); \
322 else \
323 /* Not necessary for other sizes in the moment. */ \
324 abort (); })
327 # define CALL_THREAD_FCT(descr) \
328 ({ void *__res; \
329 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
330 "callq *%%fs:%P1" \
331 : "=a" (__res) \
332 : "i" (offsetof (struct pthread, start_routine)), \
333 "i" (offsetof (struct pthread, arg)) \
334 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
335 "memory", "cc"); \
336 __res; })
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() \
360 do \
361 { int __res; \
362 asm volatile ("xchgl %0, %%fs:%P1" \
363 : "=r" (__res) \
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); \
369 while (0)
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) ()
376 # ifdef SHARED
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); \
398 while (0)
400 # define RTLD_FINALIZE_FOREIGN_CALL \
401 do { \
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); \
406 } while (0)
407 # endif
410 #endif /* __ASSEMBLER__ */
412 #endif /* tls.h */