]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/sysdeps/x86_64/tls.h
Add ceil implementation for 64-bit machines.
[thirdparty/glibc.git] / nptl / sysdeps / x86_64 / tls.h
CommitLineData
a3931336 1/* Definition for thread-local data handling. nptl/x86_64 version.
d5c157a9 2 Copyright (C) 2002-2007, 2008, 2009 Free Software Foundation, Inc.
a3931336
UD
3 This file is part of the GNU C Library.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#ifndef _TLS_H
21#define _TLS_H 1
22
23#ifndef __ASSEMBLER__
16a1d952 24# include <asm/prctl.h> /* For ARCH_SET_FS. */
9dcafc55 25# include <stdbool.h>
a3931336
UD
26# include <stddef.h>
27# include <stdint.h>
33b5d0cc 28# include <stdlib.h>
df94b641 29# include <sysdep.h>
5a8075b1 30# include <kernel-features.h>
71bb2639 31# include <bits/wordsize.h>
b48a267b 32# include <xmmintrin.h>
a3931336
UD
33
34
35/* Type for the dtv. */
36typedef union dtv
37{
38 size_t counter;
9dcafc55
UD
39 struct
40 {
41 void *val;
42 bool is_static;
43 } pointer;
a3931336
UD
44} dtv_t;
45
46
47typedef struct
48{
46d51e9b 49 void *tcb; /* Pointer to the TCB. Not necessarily the
a3931336
UD
50 thread descriptor used by libpthread. */
51 dtv_t *dtv;
52 void *self; /* Pointer to the thread descriptor. */
bbde8527 53 int multiple_threads;
df94b641 54 int gscope_flag;
35f1e827
UD
55 uintptr_t sysinfo;
56 uintptr_t stack_guard;
827b7087 57 uintptr_t pointer_guard;
46d51e9b 58 unsigned long int vgetcpu_cache[2];
b48a267b 59# ifndef __ASSUME_PRIVATE_FUTEX
5a8075b1 60 int private_futex;
b48a267b 61# else
71bb2639 62 int __unused1;
b48a267b
UD
63# endif
64# if __WORDSIZE == 64
65 int rtld_must_xmm_save;
66# endif
71bb2639
UD
67 /* Reservation of some values for the TM ABI. */
68 void *__private_tm[5];
b48a267b
UD
69# if __WORDSIZE == 64
70 long int __unused2;
71 /* Have space for the post-AVX register size. */
72 __m128 rtld_savespace_sse[8][4];
73
74 void *__padding[8];
75# endif
a3931336 76} tcbhead_t;
bbde8527 77
d0369fb8
UD
78#else /* __ASSEMBLER__ */
79# include <tcb-offsets.h>
a3931336
UD
80#endif
81
82
83/* We require TLS support in the tools. */
84#ifndef HAVE_TLS_SUPPORT
85# error "TLS support is required."
86#endif
87
a3931336
UD
88/* Alignment requirement for the stack. */
89#define STACK_ALIGN 16
90
91
92#ifndef __ASSEMBLER__
93/* Get system call information. */
94# include <sysdep.h>
95
a3931336
UD
96
97/* Get the thread descriptor definition. */
98# include <nptl/descr.h>
99
bd4f43b4
UD
100#ifndef LOCK_PREFIX
101# ifdef UP
102# define LOCK_PREFIX /* nothing */
103# else
104# define LOCK_PREFIX "lock;"
105# endif
106#endif
107
f7d78e18
UD
108/* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
109 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
110 struct pthread even when not linked with -lpthread. */
111# define TLS_INIT_TCB_SIZE sizeof (struct pthread)
a3931336
UD
112
113/* Alignment requirements for the initial TCB. */
f7d78e18 114# define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
a3931336
UD
115
116/* This is the size of the TCB. */
117# define TLS_TCB_SIZE sizeof (struct pthread)
118
119/* Alignment requirements for the TCB. */
586fa886
UD
120//# define TLS_TCB_ALIGN __alignof__ (struct pthread)
121// Normally the above would be correct But we have to store post-AVX
122// vector registers in the TCB and we want the storage to be aligned.
123// unfortunately there isn't yet a type for these values and hence no
124// 32-byte alignment requirement. Make this explicit, for now.
125# define TLS_TCB_ALIGN 32
a3931336
UD
126
127/* The TCB can have any size and the memory following the address the
128 thread pointer points to is unspecified. Allocate the TCB there. */
129# define TLS_TCB_AT_TP 1
130
131
132/* Install the dtv pointer. The pointer passed is to the element with
133 index -1 which contain the length. */
134# define INSTALL_DTV(descr, dtvp) \
d4f64e1a 135 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
a3931336
UD
136
137/* Install new dtv for current thread. */
d4f64e1a 138# define INSTALL_NEW_DTV(dtvp) \
a3931336 139 ({ struct pthread *__pd; \
55c11fbd 140 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
a3931336
UD
141
142/* Return dtv of given thread descriptor. */
143# define GET_DTV(descr) \
144 (((tcbhead_t *) (descr))->dtv)
145
146
c6180643
UD
147/* Macros to load from and store into segment registers. */
148# define TLS_GET_FS() \
149 ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
150# define TLS_SET_FS(val) \
151 __asm ("movl %0, %%fs" :: "q" (val))
152
153
a3931336
UD
154/* Code to initially initialize the thread pointer. This might need
155 special attention since 'errno' is not yet available and if the
156 operation can cause a failure 'errno' must not be touched.
157
158 We have to make the syscall for both uses of the macro since the
159 address might be (and probably is) different. */
160# define TLS_INIT_TP(thrdescr, secondcall) \
161 ({ void *_thrdescr = (thrdescr); \
162 tcbhead_t *_head = _thrdescr; \
163 int _result; \
164 \
165 _head->tcb = _thrdescr; \
166 /* For now the thread descriptor is at the same address. */ \
167 _head->self = _thrdescr; \
168 \
169 /* It is a simple syscall to set the %fs value for the thread. */ \
170 asm volatile ("syscall" \
171 : "=a" (_result) \
172 : "0" ((unsigned long int) __NR_arch_prctl), \
173 "D" ((unsigned long int) ARCH_SET_FS), \
3e976b96 174 "S" (_thrdescr) \
a3931336
UD
175 : "memory", "cc", "r11", "cx"); \
176 \
fde89ad0
RM
177 _result ? "cannot set %fs base address for thread-local storage" : 0; \
178 })
a3931336
UD
179
180
181/* Return the address of the dtv for the current thread. */
182# define THREAD_DTV() \
183 ({ struct pthread *__pd; \
55c11fbd 184 THREAD_GETMEM (__pd, header.dtv); })
a3931336
UD
185
186
187/* Return the thread descriptor for the current thread.
188
189 The contained asm must *not* be marked volatile since otherwise
190 assignments like
191 pthread_descr self = thread_self();
192 do not get optimized away. */
193# define THREAD_SELF \
194 ({ struct pthread *__self; \
5a03acfe 195 asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
55c11fbd 196 : "i" (offsetof (struct pthread, header.self))); \
a3931336
UD
197 __self;})
198
7f08f55a
RM
199/* Magic for libthread_db to know how to do THREAD_SELF. */
200# define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
201# define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
a3931336
UD
202
203/* Read member of the thread descriptor directly. */
204# define THREAD_GETMEM(descr, member) \
205 ({ __typeof (descr->member) __value; \
206 if (sizeof (__value) == 1) \
7f293686
UD
207 asm volatile ("movb %%fs:%P2,%b0" \
208 : "=q" (__value) \
209 : "0" (0), "i" (offsetof (struct pthread, member))); \
a3931336 210 else if (sizeof (__value) == 4) \
7f293686
UD
211 asm volatile ("movl %%fs:%P1,%0" \
212 : "=r" (__value) \
213 : "i" (offsetof (struct pthread, member))); \
a3931336
UD
214 else \
215 { \
216 if (sizeof (__value) != 8) \
217 /* There should not be any value with a size other than 1, \
218 4 or 8. */ \
219 abort (); \
220 \
7f293686
UD
221 asm volatile ("movq %%fs:%P1,%q0" \
222 : "=r" (__value) \
223 : "i" (offsetof (struct pthread, member))); \
a3931336
UD
224 } \
225 __value; })
226
227
228/* Same as THREAD_GETMEM, but the member offset can be non-constant. */
229# define THREAD_GETMEM_NC(descr, member, idx) \
230 ({ __typeof (descr->member[0]) __value; \
231 if (sizeof (__value) == 1) \
7f293686
UD
232 asm volatile ("movb %%fs:%P2(%q3),%b0" \
233 : "=q" (__value) \
234 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
235 "r" (idx)); \
a3931336 236 else if (sizeof (__value) == 4) \
7f293686
UD
237 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
238 : "=r" (__value) \
239 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
a3931336
UD
240 else \
241 { \
242 if (sizeof (__value) != 8) \
243 /* There should not be any value with a size other than 1, \
244 4 or 8. */ \
245 abort (); \
246 \
7f293686
UD
247 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
248 : "=r" (__value) \
249 : "i" (offsetof (struct pthread, member[0])), \
250 "r" (idx)); \
a3931336
UD
251 } \
252 __value; })
253
254
177d1ad3
UD
255/* Loading addresses of objects on x86-64 needs to be treated special
256 when generating PIC code. */
257#ifdef __pic__
258# define IMM_MODE "nr"
259#else
260# define IMM_MODE "ir"
261#endif
262
263
a3931336
UD
264/* Same as THREAD_SETMEM, but the member offset can be non-constant. */
265# define THREAD_SETMEM(descr, member, value) \
266 ({ if (sizeof (descr->member) == 1) \
5a03acfe 267 asm volatile ("movb %b0,%%fs:%P1" : \
a3931336
UD
268 : "iq" (value), \
269 "i" (offsetof (struct pthread, member))); \
270 else if (sizeof (descr->member) == 4) \
271 asm volatile ("movl %0,%%fs:%P1" : \
177d1ad3 272 : IMM_MODE (value), \
a3931336
UD
273 "i" (offsetof (struct pthread, member))); \
274 else \
275 { \
276 if (sizeof (descr->member) != 8) \
277 /* There should not be any value with a size other than 1, \
278 4 or 8. */ \
279 abort (); \
280 \
5a03acfe 281 asm volatile ("movq %q0,%%fs:%P1" : \
177d1ad3 282 : IMM_MODE ((unsigned long int) value), \
a3931336
UD
283 "i" (offsetof (struct pthread, member))); \
284 }})
285
286
287/* Set member of the thread descriptor directly. */
288# define THREAD_SETMEM_NC(descr, member, idx, value) \
289 ({ if (sizeof (descr->member[0]) == 1) \
5a03acfe 290 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
a3931336
UD
291 : "iq" (value), \
292 "i" (offsetof (struct pthread, member[0])), \
293 "r" (idx)); \
294 else if (sizeof (descr->member[0]) == 4) \
5a03acfe 295 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
177d1ad3 296 : IMM_MODE (value), \
a3931336
UD
297 "i" (offsetof (struct pthread, member[0])), \
298 "r" (idx)); \
299 else \
300 { \
301 if (sizeof (descr->member[0]) != 8) \
302 /* There should not be any value with a size other than 1, \
303 4 or 8. */ \
304 abort (); \
305 \
5a03acfe 306 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
177d1ad3 307 : IMM_MODE ((unsigned long int) value), \
a3931336
UD
308 "i" (offsetof (struct pthread, member[0])), \
309 "r" (idx)); \
310 }})
311
312
b22d701b 313/* Atomic compare and exchange on TLS, returning old value. */
b48a267b 314# define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
b22d701b
UD
315 ({ __typeof (descr->member) __ret; \
316 __typeof (oldval) __old = (oldval); \
317 if (sizeof (descr->member) == 4) \
bd4f43b4 318 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
b22d701b
UD
319 : "=a" (__ret) \
320 : "0" (__old), "r" (newval), \
321 "i" (offsetof (struct pthread, member))); \
322 else \
323 /* Not necessary for other sizes in the moment. */ \
324 abort (); \
325 __ret; })
326
327
d5c157a9 328/* Atomic logical and. */
b48a267b 329# define THREAD_ATOMIC_AND(descr, member, val) \
d5c157a9
UD
330 (void) ({ if (sizeof ((descr)->member) == 4) \
331 asm volatile (LOCK_PREFIX "andl %1, %%fs:%P0" \
332 :: "i" (offsetof (struct pthread, member)), \
333 "ir" (val)); \
334 else \
335 /* Not necessary for other sizes in the moment. */ \
336 abort (); })
337
338
b22d701b 339/* Atomic set bit. */
b48a267b 340# define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
b22d701b 341 (void) ({ if (sizeof ((descr)->member) == 4) \
bd4f43b4 342 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
b22d701b
UD
343 :: "i" (offsetof (struct pthread, member)), \
344 "ir" (1 << (bit))); \
345 else \
346 /* Not necessary for other sizes in the moment. */ \
347 abort (); })
348
349
b48a267b 350# define CALL_THREAD_FCT(descr) \
a87731e2
UD
351 ({ void *__res; \
352 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
353 "callq *%%fs:%P1" \
354 : "=a" (__res) \
355 : "i" (offsetof (struct pthread, start_routine)), \
356 "i" (offsetof (struct pthread, arg)) \
357 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
358 "memory", "cc"); \
359 __res; })
360
361
35f1e827
UD
362/* Set the stack guard field in TCB head. */
363# define THREAD_SET_STACK_GUARD(value) \
364 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
365# define THREAD_COPY_STACK_GUARD(descr) \
366 ((descr)->header.stack_guard \
367 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
368
827b7087
UD
369
370/* Set the pointer guard field in the TCB head. */
b48a267b 371# define THREAD_SET_POINTER_GUARD(value) \
827b7087 372 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
b48a267b 373# define THREAD_COPY_POINTER_GUARD(descr) \
827b7087
UD
374 ((descr)->header.pointer_guard \
375 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
376
377
df94b641 378/* Get and set the global scope generation counter in the TCB head. */
b48a267b
UD
379# define THREAD_GSCOPE_FLAG_UNUSED 0
380# define THREAD_GSCOPE_FLAG_USED 1
381# define THREAD_GSCOPE_FLAG_WAIT 2
382# define THREAD_GSCOPE_RESET_FLAG() \
df94b641
UD
383 do \
384 { int __res; \
385 asm volatile ("xchgl %0, %%fs:%P1" \
386 : "=r" (__res) \
387 : "i" (offsetof (struct pthread, header.gscope_flag)), \
388 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
389 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
085a4412 390 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
df94b641
UD
391 } \
392 while (0)
b48a267b 393# define THREAD_GSCOPE_SET_FLAG() \
df94b641 394 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
b48a267b 395# define THREAD_GSCOPE_WAIT() \
6d59823c 396 GL(dl_wait_lookup_done) ()
df94b641 397
b48a267b
UD
398
399# ifdef SHARED
400/* Defined in dl-trampoline.S. */
401extern void _dl_x86_64_save_sse (void);
402extern void _dl_x86_64_restore_sse (void);
403
404# define RTLD_CHECK_FOREIGN_CALL \
405 (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
406
407# define RTLD_ENABLE_FOREIGN_CALL \
408 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 1)
409
410# define RTLD_PREPARE_FOREIGN_CALL \
411 do if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save)) \
412 { \
413 _dl_x86_64_save_sse (); \
414 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0); \
415 } \
416 while (0)
417
418# define RTLD_FINALIZE_FOREIGN_CALL \
419 do { \
420 if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) == 0) \
421 _dl_x86_64_restore_sse (); \
422 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0); \
423 } while (0)
424# endif
425
426
a3931336
UD
427#endif /* __ASSEMBLER__ */
428
429#endif /* tls.h */