2007-06-09 Ulrich Drepper <drepper@redhat.com>
[glibc.git] / nptl / sysdeps / x86_64 / tls.h
blob4a614c02af6c022a89da805ba07c930c998d6009
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
18 02111-1307 USA. */
20 #ifndef _TLS_H
21 #define _TLS_H 1
23 #ifndef __ASSEMBLER__
24 # include <asm/prctl.h> /* For ARCH_SET_FS. */
25 # include <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
28 # include <stdlib.h>
31 /* Type for the dtv. */
32 typedef union dtv
34 size_t counter;
35 struct
37 void *val;
38 bool is_static;
39 } pointer;
40 } dtv_t;
43 typedef struct
45 void *tcb; /* Pointer to the TCB. Not necessarily the
46 thread descriptor used by libpthread. */
47 dtv_t *dtv;
48 void *self; /* Pointer to the thread descriptor. */
49 int multiple_threads;
50 int gscope_flag;
51 uintptr_t sysinfo;
52 uintptr_t stack_guard;
53 uintptr_t pointer_guard;
54 unsigned long int vgetcpu_cache[2];
55 } tcbhead_t;
57 #else /* __ASSEMBLER__ */
58 # include <tcb-offsets.h>
59 #endif
62 /* We require TLS support in the tools. */
63 #ifndef HAVE_TLS_SUPPORT
64 # error "TLS support is required."
65 #endif
67 /* Alignment requirement for the stack. */
68 #define STACK_ALIGN 16
71 #ifndef __ASSEMBLER__
72 /* Get system call information. */
73 # include <sysdep.h>
76 /* Get the thread descriptor definition. */
77 # include <nptl/descr.h>
79 #ifndef LOCK_PREFIX
80 # ifdef UP
81 # define LOCK_PREFIX /* nothing */
82 # else
83 # define LOCK_PREFIX "lock;"
84 # endif
85 #endif
87 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
88 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
89 struct pthread even when not linked with -lpthread. */
90 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
92 /* Alignment requirements for the initial TCB. */
93 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
95 /* This is the size of the TCB. */
96 # define TLS_TCB_SIZE sizeof (struct pthread)
98 /* Alignment requirements for the TCB. */
99 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
101 /* The TCB can have any size and the memory following the address the
102 thread pointer points to is unspecified. Allocate the TCB there. */
103 # define TLS_TCB_AT_TP 1
106 /* Install the dtv pointer. The pointer passed is to the element with
107 index -1 which contain the length. */
108 # define INSTALL_DTV(descr, dtvp) \
109 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
111 /* Install new dtv for current thread. */
112 # define INSTALL_NEW_DTV(dtvp) \
113 ({ struct pthread *__pd; \
114 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
116 /* Return dtv of given thread descriptor. */
117 # define GET_DTV(descr) \
118 (((tcbhead_t *) (descr))->dtv)
121 /* Macros to load from and store into segment registers. */
122 # define TLS_GET_FS() \
123 ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
124 # define TLS_SET_FS(val) \
125 __asm ("movl %0, %%fs" :: "q" (val))
128 /* Code to initially initialize the thread pointer. This might need
129 special attention since 'errno' is not yet available and if the
130 operation can cause a failure 'errno' must not be touched.
132 We have to make the syscall for both uses of the macro since the
133 address might be (and probably is) different. */
134 # define TLS_INIT_TP(thrdescr, secondcall) \
135 ({ void *_thrdescr = (thrdescr); \
136 tcbhead_t *_head = _thrdescr; \
137 int _result; \
139 _head->tcb = _thrdescr; \
140 /* For now the thread descriptor is at the same address. */ \
141 _head->self = _thrdescr; \
143 /* It is a simple syscall to set the %fs value for the thread. */ \
144 asm volatile ("syscall" \
145 : "=a" (_result) \
146 : "0" ((unsigned long int) __NR_arch_prctl), \
147 "D" ((unsigned long int) ARCH_SET_FS), \
148 "S" (_thrdescr) \
149 : "memory", "cc", "r11", "cx"); \
151 _result ? "cannot set %fs base address for thread-local storage" : 0; \
155 /* Return the address of the dtv for the current thread. */
156 # define THREAD_DTV() \
157 ({ struct pthread *__pd; \
158 THREAD_GETMEM (__pd, header.dtv); })
161 /* Return the thread descriptor for the current thread.
163 The contained asm must *not* be marked volatile since otherwise
164 assignments like
165 pthread_descr self = thread_self();
166 do not get optimized away. */
167 # define THREAD_SELF \
168 ({ struct pthread *__self; \
169 asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
170 : "i" (offsetof (struct pthread, header.self))); \
171 __self;})
173 /* Magic for libthread_db to know how to do THREAD_SELF. */
174 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
175 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
177 /* Read member of the thread descriptor directly. */
178 # define THREAD_GETMEM(descr, member) \
179 ({ __typeof (descr->member) __value; \
180 if (sizeof (__value) == 1) \
181 asm volatile ("movb %%fs:%P2,%b0" \
182 : "=q" (__value) \
183 : "0" (0), "i" (offsetof (struct pthread, member))); \
184 else if (sizeof (__value) == 4) \
185 asm volatile ("movl %%fs:%P1,%0" \
186 : "=r" (__value) \
187 : "i" (offsetof (struct pthread, member))); \
188 else \
190 if (sizeof (__value) != 8) \
191 /* There should not be any value with a size other than 1, \
192 4 or 8. */ \
193 abort (); \
195 asm volatile ("movq %%fs:%P1,%q0" \
196 : "=r" (__value) \
197 : "i" (offsetof (struct pthread, member))); \
199 __value; })
202 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
203 # define THREAD_GETMEM_NC(descr, member, idx) \
204 ({ __typeof (descr->member[0]) __value; \
205 if (sizeof (__value) == 1) \
206 asm volatile ("movb %%fs:%P2(%q3),%b0" \
207 : "=q" (__value) \
208 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
209 "r" (idx)); \
210 else if (sizeof (__value) == 4) \
211 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
212 : "=r" (__value) \
213 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
214 else \
216 if (sizeof (__value) != 8) \
217 /* There should not be any value with a size other than 1, \
218 4 or 8. */ \
219 abort (); \
221 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
222 : "=r" (__value) \
223 : "i" (offsetof (struct pthread, member[0])), \
224 "r" (idx)); \
226 __value; })
229 /* Loading addresses of objects on x86-64 needs to be treated special
230 when generating PIC code. */
231 #ifdef __pic__
232 # define IMM_MODE "nr"
233 #else
234 # define IMM_MODE "ir"
235 #endif
238 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
239 # define THREAD_SETMEM(descr, member, value) \
240 ({ if (sizeof (descr->member) == 1) \
241 asm volatile ("movb %b0,%%fs:%P1" : \
242 : "iq" (value), \
243 "i" (offsetof (struct pthread, member))); \
244 else if (sizeof (descr->member) == 4) \
245 asm volatile ("movl %0,%%fs:%P1" : \
246 : IMM_MODE (value), \
247 "i" (offsetof (struct pthread, member))); \
248 else \
250 if (sizeof (descr->member) != 8) \
251 /* There should not be any value with a size other than 1, \
252 4 or 8. */ \
253 abort (); \
255 asm volatile ("movq %q0,%%fs:%P1" : \
256 : IMM_MODE ((unsigned long int) value), \
257 "i" (offsetof (struct pthread, member))); \
261 /* Set member of the thread descriptor directly. */
262 # define THREAD_SETMEM_NC(descr, member, idx, value) \
263 ({ if (sizeof (descr->member[0]) == 1) \
264 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
265 : "iq" (value), \
266 "i" (offsetof (struct pthread, member[0])), \
267 "r" (idx)); \
268 else if (sizeof (descr->member[0]) == 4) \
269 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
270 : IMM_MODE (value), \
271 "i" (offsetof (struct pthread, member[0])), \
272 "r" (idx)); \
273 else \
275 if (sizeof (descr->member[0]) != 8) \
276 /* There should not be any value with a size other than 1, \
277 4 or 8. */ \
278 abort (); \
280 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
281 : IMM_MODE ((unsigned long int) value), \
282 "i" (offsetof (struct pthread, member[0])), \
283 "r" (idx)); \
287 /* Atomic compare and exchange on TLS, returning old value. */
288 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
289 ({ __typeof (descr->member) __ret; \
290 __typeof (oldval) __old = (oldval); \
291 if (sizeof (descr->member) == 4) \
292 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
293 : "=a" (__ret) \
294 : "0" (__old), "r" (newval), \
295 "i" (offsetof (struct pthread, member))); \
296 else \
297 /* Not necessary for other sizes in the moment. */ \
298 abort (); \
299 __ret; })
302 /* Atomic set bit. */
303 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
304 (void) ({ if (sizeof ((descr)->member) == 4) \
305 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
306 :: "i" (offsetof (struct pthread, member)), \
307 "ir" (1 << (bit))); \
308 else \
309 /* Not necessary for other sizes in the moment. */ \
310 abort (); })
313 #define CALL_THREAD_FCT(descr) \
314 ({ void *__res; \
315 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
316 "callq *%%fs:%P1" \
317 : "=a" (__res) \
318 : "i" (offsetof (struct pthread, start_routine)), \
319 "i" (offsetof (struct pthread, arg)) \
320 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
321 "memory", "cc"); \
322 __res; })
325 /* Set the stack guard field in TCB head. */
326 # define THREAD_SET_STACK_GUARD(value) \
327 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
328 # define THREAD_COPY_STACK_GUARD(descr) \
329 ((descr)->header.stack_guard \
330 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
333 /* Set the pointer guard field in the TCB head. */
334 #define THREAD_SET_POINTER_GUARD(value) \
335 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
336 #define THREAD_COPY_POINTER_GUARD(descr) \
337 ((descr)->header.pointer_guard \
338 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
341 /* Get and set the global scope generation counter in the TCB head. */
342 #define THREAD_GSCOPE_FLAG_UNUSED 0
343 #define THREAD_GSCOPE_FLAG_USED 1
344 #define THREAD_GSCOPE_FLAG_WAIT 2
345 #define THREAD_GSCOPE_RESET_FLAG() \
346 do \
347 { int __res; \
348 asm volatile ("xchgl %0, %%fs:%P1" \
349 : "=r" (__res) \
350 : "i" (offsetof (struct pthread, header.gscope_flag)), \
351 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
352 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
353 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1); \
355 while (0)
356 #define THREAD_GSCOPE_SET_FLAG() \
357 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
358 #define THREAD_GSCOPE_WAIT() \
359 GL(dl_wait_lookup_done) ()
361 #endif /* __ASSEMBLER__ */
363 #endif /* tls.h */