Updated to fedora-glibc-20061029T2155
[glibc.git] / nptl / sysdeps / x86_64 / tls.h
blob7618573e0bd68f82c11a551c68890da5a5cb7222
1 /* Definition for thread-local data handling. nptl/x86_64 version.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 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 necessary the
46 thread descriptor used by libpthread. */
47 dtv_t *dtv;
48 void *self; /* Pointer to the thread descriptor. */
49 int multiple_threads;
50 uintptr_t sysinfo;
51 uintptr_t stack_guard;
52 uintptr_t pointer_guard;
53 } tcbhead_t;
55 #else /* __ASSEMBLER__ */
56 # include <tcb-offsets.h>
57 #endif
60 /* We require TLS support in the tools. */
61 #ifndef HAVE_TLS_SUPPORT
62 # error "TLS support is required."
63 #endif
65 /* Alignment requirement for the stack. */
66 #define STACK_ALIGN 16
69 #ifndef __ASSEMBLER__
70 /* Get system call information. */
71 # include <sysdep.h>
74 /* Get the thread descriptor definition. */
75 # include <nptl/descr.h>
77 #ifndef LOCK_PREFIX
78 # ifdef UP
79 # define LOCK_PREFIX /* nothing */
80 # else
81 # define LOCK_PREFIX "lock;"
82 # endif
83 #endif
85 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
86 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
87 struct pthread even when not linked with -lpthread. */
88 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
90 /* Alignment requirements for the initial TCB. */
91 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
93 /* This is the size of the TCB. */
94 # define TLS_TCB_SIZE sizeof (struct pthread)
96 /* Alignment requirements for the TCB. */
97 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
99 /* The TCB can have any size and the memory following the address the
100 thread pointer points to is unspecified. Allocate the TCB there. */
101 # define TLS_TCB_AT_TP 1
104 /* Install the dtv pointer. The pointer passed is to the element with
105 index -1 which contain the length. */
106 # define INSTALL_DTV(descr, dtvp) \
107 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
109 /* Install new dtv for current thread. */
110 # define INSTALL_NEW_DTV(dtvp) \
111 ({ struct pthread *__pd; \
112 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
114 /* Return dtv of given thread descriptor. */
115 # define GET_DTV(descr) \
116 (((tcbhead_t *) (descr))->dtv)
119 /* Macros to load from and store into segment registers. */
120 # define TLS_GET_FS() \
121 ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
122 # define TLS_SET_FS(val) \
123 __asm ("movl %0, %%fs" :: "q" (val))
126 /* Code to initially initialize the thread pointer. This might need
127 special attention since 'errno' is not yet available and if the
128 operation can cause a failure 'errno' must not be touched.
130 We have to make the syscall for both uses of the macro since the
131 address might be (and probably is) different. */
132 # define TLS_INIT_TP(thrdescr, secondcall) \
133 ({ void *_thrdescr = (thrdescr); \
134 tcbhead_t *_head = _thrdescr; \
135 int _result; \
137 _head->tcb = _thrdescr; \
138 /* For now the thread descriptor is at the same address. */ \
139 _head->self = _thrdescr; \
141 /* It is a simple syscall to set the %fs value for the thread. */ \
142 asm volatile ("syscall" \
143 : "=a" (_result) \
144 : "0" ((unsigned long int) __NR_arch_prctl), \
145 "D" ((unsigned long int) ARCH_SET_FS), \
146 "S" (_thrdescr) \
147 : "memory", "cc", "r11", "cx"); \
149 _result ? "cannot set %fs base address for thread-local storage" : 0; \
153 /* Return the address of the dtv for the current thread. */
154 # define THREAD_DTV() \
155 ({ struct pthread *__pd; \
156 THREAD_GETMEM (__pd, header.dtv); })
159 /* Return the thread descriptor for the current thread.
161 The contained asm must *not* be marked volatile since otherwise
162 assignments like
163 pthread_descr self = thread_self();
164 do not get optimized away. */
165 # define THREAD_SELF \
166 ({ struct pthread *__self; \
167 asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
168 : "i" (offsetof (struct pthread, header.self))); \
169 __self;})
171 /* Magic for libthread_db to know how to do THREAD_SELF. */
172 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
173 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
175 /* Read member of the thread descriptor directly. */
176 # define THREAD_GETMEM(descr, member) \
177 ({ __typeof (descr->member) __value; \
178 if (sizeof (__value) == 1) \
179 asm volatile ("movb %%fs:%P2,%b0" \
180 : "=q" (__value) \
181 : "0" (0), "i" (offsetof (struct pthread, member))); \
182 else if (sizeof (__value) == 4) \
183 asm volatile ("movl %%fs:%P1,%0" \
184 : "=r" (__value) \
185 : "i" (offsetof (struct pthread, member))); \
186 else \
188 if (sizeof (__value) != 8) \
189 /* There should not be any value with a size other than 1, \
190 4 or 8. */ \
191 abort (); \
193 asm volatile ("movq %%fs:%P1,%q0" \
194 : "=r" (__value) \
195 : "i" (offsetof (struct pthread, member))); \
197 __value; })
200 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
201 # define THREAD_GETMEM_NC(descr, member, idx) \
202 ({ __typeof (descr->member[0]) __value; \
203 if (sizeof (__value) == 1) \
204 asm volatile ("movb %%fs:%P2(%q3),%b0" \
205 : "=q" (__value) \
206 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
207 "r" (idx)); \
208 else if (sizeof (__value) == 4) \
209 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
210 : "=r" (__value) \
211 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
212 else \
214 if (sizeof (__value) != 8) \
215 /* There should not be any value with a size other than 1, \
216 4 or 8. */ \
217 abort (); \
219 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
220 : "=r" (__value) \
221 : "i" (offsetof (struct pthread, member[0])), \
222 "r" (idx)); \
224 __value; })
227 /* Loading addresses of objects on x86-64 needs to be treated special
228 when generating PIC code. */
229 #ifdef __pic__
230 # define IMM_MODE "nr"
231 #else
232 # define IMM_MODE "ir"
233 #endif
236 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
237 # define THREAD_SETMEM(descr, member, value) \
238 ({ if (sizeof (descr->member) == 1) \
239 asm volatile ("movb %b0,%%fs:%P1" : \
240 : "iq" (value), \
241 "i" (offsetof (struct pthread, member))); \
242 else if (sizeof (descr->member) == 4) \
243 asm volatile ("movl %0,%%fs:%P1" : \
244 : IMM_MODE (value), \
245 "i" (offsetof (struct pthread, member))); \
246 else \
248 if (sizeof (descr->member) != 8) \
249 /* There should not be any value with a size other than 1, \
250 4 or 8. */ \
251 abort (); \
253 asm volatile ("movq %q0,%%fs:%P1" : \
254 : IMM_MODE ((unsigned long int) value), \
255 "i" (offsetof (struct pthread, member))); \
259 /* Set member of the thread descriptor directly. */
260 # define THREAD_SETMEM_NC(descr, member, idx, value) \
261 ({ if (sizeof (descr->member[0]) == 1) \
262 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
263 : "iq" (value), \
264 "i" (offsetof (struct pthread, member[0])), \
265 "r" (idx)); \
266 else if (sizeof (descr->member[0]) == 4) \
267 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
268 : IMM_MODE (value), \
269 "i" (offsetof (struct pthread, member[0])), \
270 "r" (idx)); \
271 else \
273 if (sizeof (descr->member[0]) != 8) \
274 /* There should not be any value with a size other than 1, \
275 4 or 8. */ \
276 abort (); \
278 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
279 : IMM_MODE ((unsigned long int) value), \
280 "i" (offsetof (struct pthread, member[0])), \
281 "r" (idx)); \
285 /* Atomic compare and exchange on TLS, returning old value. */
286 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
287 ({ __typeof (descr->member) __ret; \
288 __typeof (oldval) __old = (oldval); \
289 if (sizeof (descr->member) == 4) \
290 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
291 : "=a" (__ret) \
292 : "0" (__old), "r" (newval), \
293 "i" (offsetof (struct pthread, member))); \
294 else \
295 /* Not necessary for other sizes in the moment. */ \
296 abort (); \
297 __ret; })
300 /* Atomic set bit. */
301 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
302 (void) ({ if (sizeof ((descr)->member) == 4) \
303 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
304 :: "i" (offsetof (struct pthread, member)), \
305 "ir" (1 << (bit))); \
306 else \
307 /* Not necessary for other sizes in the moment. */ \
308 abort (); })
311 #define CALL_THREAD_FCT(descr) \
312 ({ void *__res; \
313 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
314 "callq *%%fs:%P1" \
315 : "=a" (__res) \
316 : "i" (offsetof (struct pthread, start_routine)), \
317 "i" (offsetof (struct pthread, arg)) \
318 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
319 "memory", "cc"); \
320 __res; })
323 /* Set the stack guard field in TCB head. */
324 # define THREAD_SET_STACK_GUARD(value) \
325 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
326 # define THREAD_COPY_STACK_GUARD(descr) \
327 ((descr)->header.stack_guard \
328 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
331 /* Set the pointer guard field in the TCB head. */
332 #define THREAD_SET_POINTER_GUARD(value) \
333 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
334 #define THREAD_COPY_POINTER_GUARD(descr) \
335 ((descr)->header.pointer_guard \
336 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
339 #endif /* __ASSEMBLER__ */
341 #endif /* tls.h */