]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/sysdeps/x86_64/tls.h
Reserve new TLS field for x86 and x86_64
[thirdparty/glibc.git] / nptl / sysdeps / x86_64 / tls.h
1 /* Definition for thread-local data handling. nptl/x86_64 version.
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
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, see
17 <http://www.gnu.org/licenses/>. */
18
19 #ifndef _TLS_H
20 #define _TLS_H 1
21
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 <libc-internal.h>
30 # include <kernel-features.h>
31
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. */
35 typedef struct
36 {
37 int i[4];
38 } __128bits;
39
40
41 /* Type for the dtv. */
42 typedef union dtv
43 {
44 size_t counter;
45 struct
46 {
47 void *val;
48 bool is_static;
49 } pointer;
50 } dtv_t;
51
52
53 typedef struct
54 {
55 void *tcb; /* Pointer to the TCB. Not necessarily the
56 thread descriptor used by libpthread. */
57 dtv_t *dtv;
58 void *self; /* Pointer to the thread descriptor. */
59 int multiple_threads;
60 int gscope_flag;
61 uintptr_t sysinfo;
62 uintptr_t stack_guard;
63 uintptr_t pointer_guard;
64 unsigned long int vgetcpu_cache[2];
65 # ifndef __ASSUME_PRIVATE_FUTEX
66 int private_futex;
67 # else
68 int __unused1;
69 # endif
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. */
74 void *__private_ss;
75 long int __unused2;
76 /* Have space for the post-AVX register size. */
77 __128bits rtld_savespace_sse[8][4] __attribute__ ((aligned (32)));
78
79 void *__padding[8];
80 } tcbhead_t;
81
82 #else /* __ASSEMBLER__ */
83 # include <tcb-offsets.h>
84 #endif
85
86
87 /* Alignment requirement for the stack. */
88 #define STACK_ALIGN 16
89
90
91 #ifndef __ASSEMBLER__
92 /* Get system call information. */
93 # include <sysdep.h>
94
95
96 /* Get the thread descriptor definition. */
97 # include <nptl/descr.h>
98
99 #ifndef LOCK_PREFIX
100 # ifdef UP
101 # define LOCK_PREFIX /* nothing */
102 # else
103 # define LOCK_PREFIX "lock;"
104 # endif
105 #endif
106
107 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
108 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
109 struct pthread even when not linked with -lpthread. */
110 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
111
112 /* Alignment requirements for the initial TCB. */
113 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
114
115 /* This is the size of the TCB. */
116 # define TLS_TCB_SIZE sizeof (struct pthread)
117
118 /* Alignment requirements for the TCB. */
119 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
120
121 /* The TCB can have any size and the memory following the address the
122 thread pointer points to is unspecified. Allocate the TCB there. */
123 # define TLS_TCB_AT_TP 1
124
125
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
130
131 /* Install new dtv for current thread. */
132 # define INSTALL_NEW_DTV(dtvp) \
133 ({ struct pthread *__pd; \
134 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
135
136 /* Return dtv of given thread descriptor. */
137 # define GET_DTV(descr) \
138 (((tcbhead_t *) (descr))->dtv)
139
140
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.
144
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, secondcall) \
148 ({ void *_thrdescr = (thrdescr); \
149 tcbhead_t *_head = _thrdescr; \
150 int _result; \
151 \
152 _head->tcb = _thrdescr; \
153 /* For now the thread descriptor is at the same address. */ \
154 _head->self = _thrdescr; \
155 \
156 /* It is a simple syscall to set the %fs value for the thread. */ \
157 asm volatile ("syscall" \
158 : "=a" (_result) \
159 : "0" ((unsigned long int) __NR_arch_prctl), \
160 "D" ((unsigned long int) ARCH_SET_FS), \
161 "S" (_thrdescr) \
162 : "memory", "cc", "r11", "cx"); \
163 \
164 _result ? "cannot set %fs base address for thread-local storage" : 0; \
165 })
166
167
168 /* Return the address of the dtv for the current thread. */
169 # define THREAD_DTV() \
170 ({ struct pthread *__pd; \
171 THREAD_GETMEM (__pd, header.dtv); })
172
173
174 /* Return the thread descriptor for the current thread.
175
176 The contained asm must *not* be marked volatile since otherwise
177 assignments like
178 pthread_descr self = thread_self();
179 do not get optimized away. */
180 # define THREAD_SELF \
181 ({ struct pthread *__self; \
182 asm ("mov %%fs:%c1,%0" : "=r" (__self) \
183 : "i" (offsetof (struct pthread, header.self))); \
184 __self;})
185
186 /* Magic for libthread_db to know how to do THREAD_SELF. */
187 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
188 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
189
190 /* Read member of the thread descriptor directly. */
191 # define THREAD_GETMEM(descr, member) \
192 ({ __typeof (descr->member) __value; \
193 if (sizeof (__value) == 1) \
194 asm volatile ("movb %%fs:%P2,%b0" \
195 : "=q" (__value) \
196 : "0" (0), "i" (offsetof (struct pthread, member))); \
197 else if (sizeof (__value) == 4) \
198 asm volatile ("movl %%fs:%P1,%0" \
199 : "=r" (__value) \
200 : "i" (offsetof (struct pthread, member))); \
201 else \
202 { \
203 if (sizeof (__value) != 8) \
204 /* There should not be any value with a size other than 1, \
205 4 or 8. */ \
206 abort (); \
207 \
208 asm volatile ("movq %%fs:%P1,%q0" \
209 : "=r" (__value) \
210 : "i" (offsetof (struct pthread, member))); \
211 } \
212 __value; })
213
214
215 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
216 # define THREAD_GETMEM_NC(descr, member, idx) \
217 ({ __typeof (descr->member[0]) __value; \
218 if (sizeof (__value) == 1) \
219 asm volatile ("movb %%fs:%P2(%q3),%b0" \
220 : "=q" (__value) \
221 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
222 "r" (idx)); \
223 else if (sizeof (__value) == 4) \
224 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
225 : "=r" (__value) \
226 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
227 else \
228 { \
229 if (sizeof (__value) != 8) \
230 /* There should not be any value with a size other than 1, \
231 4 or 8. */ \
232 abort (); \
233 \
234 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
235 : "=r" (__value) \
236 : "i" (offsetof (struct pthread, member[0])), \
237 "r" (idx)); \
238 } \
239 __value; })
240
241
242 /* Loading addresses of objects on x86-64 needs to be treated special
243 when generating PIC code. */
244 #ifdef __pic__
245 # define IMM_MODE "nr"
246 #else
247 # define IMM_MODE "ir"
248 #endif
249
250
251 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
252 # define THREAD_SETMEM(descr, member, value) \
253 ({ if (sizeof (descr->member) == 1) \
254 asm volatile ("movb %b0,%%fs:%P1" : \
255 : "iq" (value), \
256 "i" (offsetof (struct pthread, member))); \
257 else if (sizeof (descr->member) == 4) \
258 asm volatile ("movl %0,%%fs:%P1" : \
259 : IMM_MODE (value), \
260 "i" (offsetof (struct pthread, member))); \
261 else \
262 { \
263 if (sizeof (descr->member) != 8) \
264 /* There should not be any value with a size other than 1, \
265 4 or 8. */ \
266 abort (); \
267 \
268 asm volatile ("movq %q0,%%fs:%P1" : \
269 : IMM_MODE ((uint64_t) cast_to_integer (value)), \
270 "i" (offsetof (struct pthread, member))); \
271 }})
272
273
274 /* Set member of the thread descriptor directly. */
275 # define THREAD_SETMEM_NC(descr, member, idx, value) \
276 ({ if (sizeof (descr->member[0]) == 1) \
277 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
278 : "iq" (value), \
279 "i" (offsetof (struct pthread, member[0])), \
280 "r" (idx)); \
281 else if (sizeof (descr->member[0]) == 4) \
282 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
283 : IMM_MODE (value), \
284 "i" (offsetof (struct pthread, member[0])), \
285 "r" (idx)); \
286 else \
287 { \
288 if (sizeof (descr->member[0]) != 8) \
289 /* There should not be any value with a size other than 1, \
290 4 or 8. */ \
291 abort (); \
292 \
293 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
294 : IMM_MODE ((uint64_t) cast_to_integer (value)), \
295 "i" (offsetof (struct pthread, member[0])), \
296 "r" (idx)); \
297 }})
298
299
300 /* Atomic compare and exchange on TLS, returning old value. */
301 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
302 ({ __typeof (descr->member) __ret; \
303 __typeof (oldval) __old = (oldval); \
304 if (sizeof (descr->member) == 4) \
305 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
306 : "=a" (__ret) \
307 : "0" (__old), "r" (newval), \
308 "i" (offsetof (struct pthread, member))); \
309 else \
310 /* Not necessary for other sizes in the moment. */ \
311 abort (); \
312 __ret; })
313
314
315 /* Atomic logical and. */
316 # define THREAD_ATOMIC_AND(descr, member, val) \
317 (void) ({ if (sizeof ((descr)->member) == 4) \
318 asm volatile (LOCK_PREFIX "andl %1, %%fs:%P0" \
319 :: "i" (offsetof (struct pthread, member)), \
320 "ir" (val)); \
321 else \
322 /* Not necessary for other sizes in the moment. */ \
323 abort (); })
324
325
326 /* Atomic set bit. */
327 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
328 (void) ({ if (sizeof ((descr)->member) == 4) \
329 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
330 :: "i" (offsetof (struct pthread, member)), \
331 "ir" (1 << (bit))); \
332 else \
333 /* Not necessary for other sizes in the moment. */ \
334 abort (); })
335
336
337 # define CALL_THREAD_FCT(descr) \
338 ({ void *__res; \
339 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
340 "callq *%%fs:%P1" \
341 : "=a" (__res) \
342 : "i" (offsetof (struct pthread, start_routine)), \
343 "i" (offsetof (struct pthread, arg)) \
344 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
345 "memory", "cc"); \
346 __res; })
347
348
349 /* Set the stack guard field in TCB head. */
350 # define THREAD_SET_STACK_GUARD(value) \
351 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
352 # define THREAD_COPY_STACK_GUARD(descr) \
353 ((descr)->header.stack_guard \
354 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
355
356
357 /* Set the pointer guard field in the TCB head. */
358 # define THREAD_SET_POINTER_GUARD(value) \
359 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
360 # define THREAD_COPY_POINTER_GUARD(descr) \
361 ((descr)->header.pointer_guard \
362 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
363
364
365 /* Get and set the global scope generation counter in the TCB head. */
366 # define THREAD_GSCOPE_FLAG_UNUSED 0
367 # define THREAD_GSCOPE_FLAG_USED 1
368 # define THREAD_GSCOPE_FLAG_WAIT 2
369 # define THREAD_GSCOPE_RESET_FLAG() \
370 do \
371 { int __res; \
372 asm volatile ("xchgl %0, %%fs:%P1" \
373 : "=r" (__res) \
374 : "i" (offsetof (struct pthread, header.gscope_flag)), \
375 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
376 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
377 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
378 } \
379 while (0)
380 # define THREAD_GSCOPE_SET_FLAG() \
381 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
382 # define THREAD_GSCOPE_WAIT() \
383 GL(dl_wait_lookup_done) ()
384
385
386 # ifdef SHARED
387 /* Defined in dl-trampoline.S. */
388 extern void _dl_x86_64_save_sse (void);
389 extern void _dl_x86_64_restore_sse (void);
390
391 # define RTLD_CHECK_FOREIGN_CALL \
392 (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
393
394 /* NB: Don't use the xchg operation because that would imply a lock
395 prefix which is expensive and unnecessary. The cache line is also
396 not contested at all. */
397 # define RTLD_ENABLE_FOREIGN_CALL \
398 int old_rtld_must_xmm_save = THREAD_GETMEM (THREAD_SELF, \
399 header.rtld_must_xmm_save); \
400 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 1)
401
402 # define RTLD_PREPARE_FOREIGN_CALL \
403 do if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save)) \
404 { \
405 _dl_x86_64_save_sse (); \
406 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0); \
407 } \
408 while (0)
409
410 # define RTLD_FINALIZE_FOREIGN_CALL \
411 do { \
412 if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) == 0) \
413 _dl_x86_64_restore_sse (); \
414 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, \
415 old_rtld_must_xmm_save); \
416 } while (0)
417 # endif
418
419
420 #endif /* __ASSEMBLER__ */
421
422 #endif /* tls.h */