]>
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. */
31 /* Type for the dtv. */
45 void *tcb
; /* Pointer to the TCB. Not necessary the
46 thread descriptor used by libpthread. */
48 void *self
; /* Pointer to the thread descriptor. */
52 uintptr_t stack_guard
;
53 uintptr_t pointer_guard
;
56 #else /* __ASSEMBLER__ */
57 # include <tcb-offsets.h>
61 /* We require TLS support in the tools. */
62 #ifndef HAVE_TLS_SUPPORT
63 # error "TLS support is required."
66 /* Signal that TLS support is available. */
69 /* Alignment requirement for the stack. */
70 #define STACK_ALIGN 16
74 /* Get system call information. */
78 /* Get the thread descriptor definition. */
79 # include <nptl/descr.h>
83 # define LOCK_PREFIX /* nothing */
85 # define LOCK_PREFIX "lock;"
89 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
90 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
91 struct pthread even when not linked with -lpthread. */
92 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
94 /* Alignment requirements for the initial TCB. */
95 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
97 /* This is the size of the TCB. */
98 # define TLS_TCB_SIZE sizeof (struct pthread)
100 /* Alignment requirements for the TCB. */
101 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
103 /* The TCB can have any size and the memory following the address the
104 thread pointer points to is unspecified. Allocate the TCB there. */
105 # define TLS_TCB_AT_TP 1
108 /* Install the dtv pointer. The pointer passed is to the element with
109 index -1 which contain the length. */
110 # define INSTALL_DTV(descr, dtvp) \
111 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
113 /* Install new dtv for current thread. */
114 # define INSTALL_NEW_DTV(dtvp) \
115 ({ struct pthread *__pd; \
116 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
118 /* Return dtv of given thread descriptor. */
119 # define GET_DTV(descr) \
120 (((tcbhead_t *) (descr))->dtv)
123 /* Macros to load from and store into segment registers. */
124 # define TLS_GET_FS() \
125 ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
126 # define TLS_SET_FS(val) \
127 __asm ("movl %0, %%fs" :: "q" (val))
130 /* Code to initially initialize the thread pointer. This might need
131 special attention since 'errno' is not yet available and if the
132 operation can cause a failure 'errno' must not be touched.
134 We have to make the syscall for both uses of the macro since the
135 address might be (and probably is) different. */
136 # define TLS_INIT_TP(thrdescr, secondcall) \
137 ({ void *_thrdescr = (thrdescr); \
138 tcbhead_t *_head = _thrdescr; \
141 _head->tcb = _thrdescr; \
142 /* For now the thread descriptor is at the same address. */ \
143 _head->self = _thrdescr; \
145 /* It is a simple syscall to set the %fs value for the thread. */ \
146 asm volatile ("syscall" \
148 : "0" ((unsigned long int) __NR_arch_prctl), \
149 "D" ((unsigned long int) ARCH_SET_FS), \
151 : "memory", "cc", "r11", "cx"); \
153 _result ? "cannot set %fs base address for thread-local storage" : 0; \
157 /* Return the address of the dtv for the current thread. */
158 # define THREAD_DTV() \
159 ({ struct pthread *__pd; \
160 THREAD_GETMEM (__pd, header.dtv); })
163 /* Return the thread descriptor for the current thread.
165 The contained asm must *not* be marked volatile since otherwise
167 pthread_descr self = thread_self();
168 do not get optimized away. */
169 # define THREAD_SELF \
170 ({ struct pthread *__self; \
171 asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
172 : "i" (offsetof (struct pthread, header.self))); \
175 /* Magic for libthread_db to know how to do THREAD_SELF. */
176 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
177 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
179 /* Read member of the thread descriptor directly. */
180 # define THREAD_GETMEM(descr, member) \
181 ({ __typeof (descr->member) __value; \
182 if (sizeof (__value) == 1) \
183 asm volatile ("movb %%fs:%P2,%b0" \
185 : "0" (0), "i" (offsetof (struct pthread, member))); \
186 else if (sizeof (__value) == 4) \
187 asm volatile ("movl %%fs:%P1,%0" \
189 : "i" (offsetof (struct pthread, member))); \
192 if (sizeof (__value) != 8) \
193 /* There should not be any value with a size other than 1, \
197 asm volatile ("movq %%fs:%P1,%q0" \
199 : "i" (offsetof (struct pthread, member))); \
204 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
205 # define THREAD_GETMEM_NC(descr, member, idx) \
206 ({ __typeof (descr->member[0]) __value; \
207 if (sizeof (__value) == 1) \
208 asm volatile ("movb %%fs:%P2(%q3),%b0" \
210 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
212 else if (sizeof (__value) == 4) \
213 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
215 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
218 if (sizeof (__value) != 8) \
219 /* There should not be any value with a size other than 1, \
223 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
225 : "i" (offsetof (struct pthread, member[0])), \
231 /* Loading addresses of objects on x86-64 needs to be treated special
232 when generating PIC code. */
234 # define IMM_MODE "nr"
236 # define IMM_MODE "ir"
240 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
241 # define THREAD_SETMEM(descr, member, value) \
242 ({ if (sizeof (descr->member) == 1) \
243 asm volatile ("movb %b0,%%fs:%P1" : \
245 "i" (offsetof (struct pthread, member))); \
246 else if (sizeof (descr->member) == 4) \
247 asm volatile ("movl %0,%%fs:%P1" : \
248 : IMM_MODE (value), \
249 "i" (offsetof (struct pthread, member))); \
252 if (sizeof (descr->member) != 8) \
253 /* There should not be any value with a size other than 1, \
257 asm volatile ("movq %q0,%%fs:%P1" : \
258 : IMM_MODE ((unsigned long int) value), \
259 "i" (offsetof (struct pthread, member))); \
263 /* Set member of the thread descriptor directly. */
264 # define THREAD_SETMEM_NC(descr, member, idx, value) \
265 ({ if (sizeof (descr->member[0]) == 1) \
266 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
268 "i" (offsetof (struct pthread, member[0])), \
270 else if (sizeof (descr->member[0]) == 4) \
271 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
272 : IMM_MODE (value), \
273 "i" (offsetof (struct pthread, member[0])), \
277 if (sizeof (descr->member[0]) != 8) \
278 /* There should not be any value with a size other than 1, \
282 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
283 : IMM_MODE ((unsigned long int) value), \
284 "i" (offsetof (struct pthread, member[0])), \
289 /* Atomic compare and exchange on TLS, returning old value. */
290 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
291 ({ __typeof (descr->member) __ret; \
292 __typeof (oldval) __old = (oldval); \
293 if (sizeof (descr->member) == 4) \
294 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
296 : "0" (__old), "r" (newval), \
297 "i" (offsetof (struct pthread, member))); \
299 /* Not necessary for other sizes in the moment. */ \
304 /* Atomic set bit. */
305 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
306 (void) ({ if (sizeof ((descr)->member) == 4) \
307 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
308 :: "i" (offsetof (struct pthread, member)), \
309 "ir" (1 << (bit))); \
311 /* Not necessary for other sizes in the moment. */ \
315 #define CALL_THREAD_FCT(descr) \
317 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
320 : "i" (offsetof (struct pthread, start_routine)), \
321 "i" (offsetof (struct pthread, arg)) \
322 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
327 /* Set the stack guard field in TCB head. */
328 # define THREAD_SET_STACK_GUARD(value) \
329 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
330 # define THREAD_COPY_STACK_GUARD(descr) \
331 ((descr)->header.stack_guard \
332 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
335 /* Set the pointer guard field in the TCB head. */
336 #define THREAD_SET_POINTER_GUARD(value) \
337 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
338 #define THREAD_COPY_POINTER_GUARD(descr) \
339 ((descr)->header.pointer_guard \
340 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
343 /* Get and set the global scope generation counter in the TCB head. */
344 #define THREAD_GSCOPE_FLAG_UNUSED 0
345 #define THREAD_GSCOPE_FLAG_USED 1
346 #define THREAD_GSCOPE_FLAG_WAIT 2
347 #define THREAD_GSCOPE_RESET_FLAG() \
350 asm volatile ("xchgl %0, %%fs:%P1" \
352 : "i" (offsetof (struct pthread, header.gscope_flag)), \
353 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
354 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
355 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1); \
358 #define THREAD_GSCOPE_SET_FLAG() \
359 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
360 #define THREAD_GSCOPE_WAIT() \
361 GL(dl_wait_lookup_done) ()
363 #endif /* __ASSEMBLER__ */