]>
git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/sysdeps/x86_64/tls.h
1 /* Definition for thread-local data handling. nptl/x86_64 version.
2 Copyright (C) 2002,2003,2004,2005,2006,2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 # include <asm/prctl.h> /* For ARCH_SET_FS. */
30 # include <kernel-features.h>
33 /* Type for the dtv. */
47 void *tcb
; /* Pointer to the TCB. Not necessarily the
48 thread descriptor used by libpthread. */
50 void *self
; /* Pointer to the thread descriptor. */
54 uintptr_t stack_guard
;
55 uintptr_t pointer_guard
;
56 unsigned long int vgetcpu_cache
[2];
57 #ifndef __ASSUME_PRIVATE_FUTEX
62 #else /* __ASSEMBLER__ */
63 # include <tcb-offsets.h>
67 /* We require TLS support in the tools. */
68 #ifndef HAVE_TLS_SUPPORT
69 # error "TLS support is required."
72 /* Alignment requirement for the stack. */
73 #define STACK_ALIGN 16
77 /* Get system call information. */
81 /* Get the thread descriptor definition. */
82 # include <nptl/descr.h>
86 # define LOCK_PREFIX /* nothing */
88 # define LOCK_PREFIX "lock;"
92 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
93 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
94 struct pthread even when not linked with -lpthread. */
95 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
97 /* Alignment requirements for the initial TCB. */
98 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
100 /* This is the size of the TCB. */
101 # define TLS_TCB_SIZE sizeof (struct pthread)
103 /* Alignment requirements for the TCB. */
104 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
106 /* The TCB can have any size and the memory following the address the
107 thread pointer points to is unspecified. Allocate the TCB there. */
108 # define TLS_TCB_AT_TP 1
111 /* Install the dtv pointer. The pointer passed is to the element with
112 index -1 which contain the length. */
113 # define INSTALL_DTV(descr, dtvp) \
114 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
116 /* Install new dtv for current thread. */
117 # define INSTALL_NEW_DTV(dtvp) \
118 ({ struct pthread *__pd; \
119 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
121 /* Return dtv of given thread descriptor. */
122 # define GET_DTV(descr) \
123 (((tcbhead_t *) (descr))->dtv)
126 /* Macros to load from and store into segment registers. */
127 # define TLS_GET_FS() \
128 ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
129 # define TLS_SET_FS(val) \
130 __asm ("movl %0, %%fs" :: "q" (val))
133 /* Code to initially initialize the thread pointer. This might need
134 special attention since 'errno' is not yet available and if the
135 operation can cause a failure 'errno' must not be touched.
137 We have to make the syscall for both uses of the macro since the
138 address might be (and probably is) different. */
139 # define TLS_INIT_TP(thrdescr, secondcall) \
140 ({ void *_thrdescr = (thrdescr); \
141 tcbhead_t *_head = _thrdescr; \
144 _head->tcb = _thrdescr; \
145 /* For now the thread descriptor is at the same address. */ \
146 _head->self = _thrdescr; \
148 /* It is a simple syscall to set the %fs value for the thread. */ \
149 asm volatile ("syscall" \
151 : "0" ((unsigned long int) __NR_arch_prctl), \
152 "D" ((unsigned long int) ARCH_SET_FS), \
154 : "memory", "cc", "r11", "cx"); \
156 _result ? "cannot set %fs base address for thread-local storage" : 0; \
160 /* Return the address of the dtv for the current thread. */
161 # define THREAD_DTV() \
162 ({ struct pthread *__pd; \
163 THREAD_GETMEM (__pd, header.dtv); })
166 /* Return the thread descriptor for the current thread.
168 The contained asm must *not* be marked volatile since otherwise
170 pthread_descr self = thread_self();
171 do not get optimized away. */
172 # define THREAD_SELF \
173 ({ struct pthread *__self; \
174 asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
175 : "i" (offsetof (struct pthread, header.self))); \
178 /* Magic for libthread_db to know how to do THREAD_SELF. */
179 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
180 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
182 /* Read member of the thread descriptor directly. */
183 # define THREAD_GETMEM(descr, member) \
184 ({ __typeof (descr->member) __value; \
185 if (sizeof (__value) == 1) \
186 asm volatile ("movb %%fs:%P2,%b0" \
188 : "0" (0), "i" (offsetof (struct pthread, member))); \
189 else if (sizeof (__value) == 4) \
190 asm volatile ("movl %%fs:%P1,%0" \
192 : "i" (offsetof (struct pthread, member))); \
195 if (sizeof (__value) != 8) \
196 /* There should not be any value with a size other than 1, \
200 asm volatile ("movq %%fs:%P1,%q0" \
202 : "i" (offsetof (struct pthread, member))); \
207 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
208 # define THREAD_GETMEM_NC(descr, member, idx) \
209 ({ __typeof (descr->member[0]) __value; \
210 if (sizeof (__value) == 1) \
211 asm volatile ("movb %%fs:%P2(%q3),%b0" \
213 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
215 else if (sizeof (__value) == 4) \
216 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
218 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
221 if (sizeof (__value) != 8) \
222 /* There should not be any value with a size other than 1, \
226 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
228 : "i" (offsetof (struct pthread, member[0])), \
234 /* Loading addresses of objects on x86-64 needs to be treated special
235 when generating PIC code. */
237 # define IMM_MODE "nr"
239 # define IMM_MODE "ir"
243 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
244 # define THREAD_SETMEM(descr, member, value) \
245 ({ if (sizeof (descr->member) == 1) \
246 asm volatile ("movb %b0,%%fs:%P1" : \
248 "i" (offsetof (struct pthread, member))); \
249 else if (sizeof (descr->member) == 4) \
250 asm volatile ("movl %0,%%fs:%P1" : \
251 : IMM_MODE (value), \
252 "i" (offsetof (struct pthread, member))); \
255 if (sizeof (descr->member) != 8) \
256 /* There should not be any value with a size other than 1, \
260 asm volatile ("movq %q0,%%fs:%P1" : \
261 : IMM_MODE ((unsigned long int) value), \
262 "i" (offsetof (struct pthread, member))); \
266 /* Set member of the thread descriptor directly. */
267 # define THREAD_SETMEM_NC(descr, member, idx, value) \
268 ({ if (sizeof (descr->member[0]) == 1) \
269 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
271 "i" (offsetof (struct pthread, member[0])), \
273 else if (sizeof (descr->member[0]) == 4) \
274 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
275 : IMM_MODE (value), \
276 "i" (offsetof (struct pthread, member[0])), \
280 if (sizeof (descr->member[0]) != 8) \
281 /* There should not be any value with a size other than 1, \
285 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
286 : IMM_MODE ((unsigned long int) value), \
287 "i" (offsetof (struct pthread, member[0])), \
292 /* Atomic compare and exchange on TLS, returning old value. */
293 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
294 ({ __typeof (descr->member) __ret; \
295 __typeof (oldval) __old = (oldval); \
296 if (sizeof (descr->member) == 4) \
297 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
299 : "0" (__old), "r" (newval), \
300 "i" (offsetof (struct pthread, member))); \
302 /* Not necessary for other sizes in the moment. */ \
307 /* Atomic set bit. */
308 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
309 (void) ({ if (sizeof ((descr)->member) == 4) \
310 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
311 :: "i" (offsetof (struct pthread, member)), \
312 "ir" (1 << (bit))); \
314 /* Not necessary for other sizes in the moment. */ \
318 #define CALL_THREAD_FCT(descr) \
320 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
323 : "i" (offsetof (struct pthread, start_routine)), \
324 "i" (offsetof (struct pthread, arg)) \
325 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
330 /* Set the stack guard field in TCB head. */
331 # define THREAD_SET_STACK_GUARD(value) \
332 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
333 # define THREAD_COPY_STACK_GUARD(descr) \
334 ((descr)->header.stack_guard \
335 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
338 /* Set the pointer guard field in the TCB head. */
339 #define THREAD_SET_POINTER_GUARD(value) \
340 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
341 #define THREAD_COPY_POINTER_GUARD(descr) \
342 ((descr)->header.pointer_guard \
343 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
346 /* Get and set the global scope generation counter in the TCB head. */
347 #define THREAD_GSCOPE_FLAG_UNUSED 0
348 #define THREAD_GSCOPE_FLAG_USED 1
349 #define THREAD_GSCOPE_FLAG_WAIT 2
350 #define THREAD_GSCOPE_RESET_FLAG() \
353 asm volatile ("xchgl %0, %%fs:%P1" \
355 : "i" (offsetof (struct pthread, header.gscope_flag)), \
356 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
357 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
358 lll_private_futex_wake (&THREAD_SELF->header.gscope_flag, 1); \
361 #define THREAD_GSCOPE_SET_FLAG() \
362 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
363 #define THREAD_GSCOPE_WAIT() \
364 GL(dl_wait_lookup_done) ()
366 #endif /* __ASSEMBLER__ */