(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / sysdeps / x86_64 / tls.h
blob3d6111f4e3fb89a989c7b5ab33da9cff07302f5a
1 /* Definition for thread-local data handling. nptl/x86_64 version.
2 Copyright (C) 2002, 2003 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 #include <asm/prctl.h> /* For ARCH_SET_FS. */
24 #ifndef __ASSEMBLER__
25 # include <stddef.h>
26 # include <stdint.h>
27 # include <stdlib.h>
30 /* Type for the dtv. */
31 typedef union dtv
33 size_t counter;
34 void *pointer;
35 } dtv_t;
38 typedef struct
40 void *tcb; /* Pointer to the TCB. Not necessary the
41 thread descriptor used by libpthread. */
42 dtv_t *dtv;
43 void *self; /* Pointer to the thread descriptor. */
44 int multiple_threads;
45 } tcbhead_t;
47 #else /* __ASSEMBLER__ */
48 # include <tcb-offsets.h>
49 #endif
52 /* We require TLS support in the tools. */
53 #ifndef HAVE_TLS_SUPPORT
54 # error "TLS support is required."
55 #endif
57 /* Signal that TLS support is available. */
58 #define USE_TLS 1
60 /* Alignment requirement for the stack. */
61 #define STACK_ALIGN 16
64 #ifndef __ASSEMBLER__
65 /* Get system call information. */
66 # include <sysdep.h>
69 /* Get the thread descriptor definition. */
70 # include <nptl/descr.h>
72 #ifndef LOCK_PREFIX
73 # ifdef UP
74 # define LOCK_PREFIX /* nothing */
75 # else
76 # define LOCK_PREFIX "lock;"
77 # endif
78 #endif
80 /* This is the size of the initial TCB. */
81 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
83 /* Alignment requirements for the initial TCB. */
84 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
86 /* This is the size of the TCB. */
87 # define TLS_TCB_SIZE sizeof (struct pthread)
89 /* Alignment requirements for the TCB. */
90 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
92 /* The TCB can have any size and the memory following the address the
93 thread pointer points to is unspecified. Allocate the TCB there. */
94 # define TLS_TCB_AT_TP 1
97 /* Install the dtv pointer. The pointer passed is to the element with
98 index -1 which contain the length. */
99 # define INSTALL_DTV(descr, dtvp) \
100 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
102 /* Install new dtv for current thread. */
103 # define INSTALL_NEW_DTV(dtvp) \
104 ({ struct pthread *__pd; \
105 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
107 /* Return dtv of given thread descriptor. */
108 # define GET_DTV(descr) \
109 (((tcbhead_t *) (descr))->dtv)
112 /* Macros to load from and store into segment registers. */
113 # define TLS_GET_FS() \
114 ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
115 # define TLS_SET_FS(val) \
116 __asm ("movl %0, %%fs" :: "q" (val))
119 /* Code to initially initialize the thread pointer. This might need
120 special attention since 'errno' is not yet available and if the
121 operation can cause a failure 'errno' must not be touched.
123 We have to make the syscall for both uses of the macro since the
124 address might be (and probably is) different. */
125 # define TLS_INIT_TP(thrdescr, secondcall) \
126 ({ void *_thrdescr = (thrdescr); \
127 tcbhead_t *_head = _thrdescr; \
128 int _result; \
130 _head->tcb = _thrdescr; \
131 /* For now the thread descriptor is at the same address. */ \
132 _head->self = _thrdescr; \
134 /* It is a simple syscall to set the %fs value for the thread. */ \
135 asm volatile ("syscall" \
136 : "=a" (_result) \
137 : "0" ((unsigned long int) __NR_arch_prctl), \
138 "D" ((unsigned long int) ARCH_SET_FS), \
139 "S" (_thrdescr) \
140 : "memory", "cc", "r11", "cx"); \
142 _result ? "cannot set %fs base address for thread-local storage" : 0; \
146 /* Return the address of the dtv for the current thread. */
147 # define THREAD_DTV() \
148 ({ struct pthread *__pd; \
149 THREAD_GETMEM (__pd, header.dtv); })
152 /* Return the thread descriptor for the current thread.
154 The contained asm must *not* be marked volatile since otherwise
155 assignments like
156 pthread_descr self = thread_self();
157 do not get optimized away. */
158 # define THREAD_SELF \
159 ({ struct pthread *__self; \
160 asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
161 : "i" (offsetof (struct pthread, header.self))); \
162 __self;})
164 /* Magic for libthread_db to know how to do THREAD_SELF. */
165 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
166 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
168 /* Read member of the thread descriptor directly. */
169 # define THREAD_GETMEM(descr, member) \
170 ({ __typeof (descr->member) __value; \
171 if (sizeof (__value) == 1) \
172 asm volatile ("movb %%fs:%P2,%b0" \
173 : "=q" (__value) \
174 : "0" (0), "i" (offsetof (struct pthread, member))); \
175 else if (sizeof (__value) == 4) \
176 asm volatile ("movl %%fs:%P1,%0" \
177 : "=r" (__value) \
178 : "i" (offsetof (struct pthread, member))); \
179 else \
181 if (sizeof (__value) != 8) \
182 /* There should not be any value with a size other than 1, \
183 4 or 8. */ \
184 abort (); \
186 asm volatile ("movq %%fs:%P1,%q0" \
187 : "=r" (__value) \
188 : "i" (offsetof (struct pthread, member))); \
190 __value; })
193 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
194 # define THREAD_GETMEM_NC(descr, member, idx) \
195 ({ __typeof (descr->member[0]) __value; \
196 if (sizeof (__value) == 1) \
197 asm volatile ("movb %%fs:%P2(%q3),%b0" \
198 : "=q" (__value) \
199 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
200 "r" (idx)); \
201 else if (sizeof (__value) == 4) \
202 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
203 : "=r" (__value) \
204 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
205 else \
207 if (sizeof (__value) != 8) \
208 /* There should not be any value with a size other than 1, \
209 4 or 8. */ \
210 abort (); \
212 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
213 : "=r" (__value) \
214 : "i" (offsetof (struct pthread, member[0])), \
215 "r" (idx)); \
217 __value; })
220 /* Loading addresses of objects on x86-64 needs to be treated special
221 when generating PIC code. */
222 #ifdef __pic__
223 # define IMM_MODE "nr"
224 #else
225 # define IMM_MODE "ir"
226 #endif
229 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
230 # define THREAD_SETMEM(descr, member, value) \
231 ({ if (sizeof (descr->member) == 1) \
232 asm volatile ("movb %b0,%%fs:%P1" : \
233 : "iq" (value), \
234 "i" (offsetof (struct pthread, member))); \
235 else if (sizeof (descr->member) == 4) \
236 asm volatile ("movl %0,%%fs:%P1" : \
237 : IMM_MODE (value), \
238 "i" (offsetof (struct pthread, member))); \
239 else \
241 if (sizeof (descr->member) != 8) \
242 /* There should not be any value with a size other than 1, \
243 4 or 8. */ \
244 abort (); \
246 asm volatile ("movq %q0,%%fs:%P1" : \
247 : IMM_MODE ((unsigned long int) value), \
248 "i" (offsetof (struct pthread, member))); \
252 /* Set member of the thread descriptor directly. */
253 # define THREAD_SETMEM_NC(descr, member, idx, value) \
254 ({ if (sizeof (descr->member[0]) == 1) \
255 asm volatile ("movb %b0,%%fs:%P1(%q2)" : \
256 : "iq" (value), \
257 "i" (offsetof (struct pthread, member[0])), \
258 "r" (idx)); \
259 else if (sizeof (descr->member[0]) == 4) \
260 asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \
261 : IMM_MODE (value), \
262 "i" (offsetof (struct pthread, member[0])), \
263 "r" (idx)); \
264 else \
266 if (sizeof (descr->member[0]) != 8) \
267 /* There should not be any value with a size other than 1, \
268 4 or 8. */ \
269 abort (); \
271 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
272 : IMM_MODE ((unsigned long int) value), \
273 "i" (offsetof (struct pthread, member[0])), \
274 "r" (idx)); \
278 /* Atomic compare and exchange on TLS, returning old value. */
279 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
280 ({ __typeof (descr->member) __ret; \
281 __typeof (oldval) __old = (oldval); \
282 if (sizeof (descr->member) == 4) \
283 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
284 : "=a" (__ret) \
285 : "0" (__old), "r" (newval), \
286 "i" (offsetof (struct pthread, member))); \
287 else \
288 /* Not necessary for other sizes in the moment. */ \
289 abort (); \
290 __ret; })
293 /* Atomic set bit. */
294 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
295 (void) ({ if (sizeof ((descr)->member) == 4) \
296 asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \
297 :: "i" (offsetof (struct pthread, member)), \
298 "ir" (1 << (bit))); \
299 else \
300 /* Not necessary for other sizes in the moment. */ \
301 abort (); })
304 #define CALL_THREAD_FCT(descr) \
305 ({ void *__res; \
306 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
307 "callq *%%fs:%P1" \
308 : "=a" (__res) \
309 : "i" (offsetof (struct pthread, start_routine)), \
310 "i" (offsetof (struct pthread, arg)) \
311 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
312 "memory", "cc"); \
313 __res; })
316 #endif /* __ASSEMBLER__ */
318 #endif /* tls.h */