]>
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 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. */
51 uintptr_t stack_guard
;
52 uintptr_t pointer_guard
;
55 #else /* __ASSEMBLER__ */
56 # include <tcb-offsets.h>
60 /* We require TLS support in the tools. */
61 #ifndef HAVE_TLS_SUPPORT
62 # error "TLS support is required."
65 /* Alignment requirement for the stack. */
66 #define STACK_ALIGN 16
70 /* Get system call information. */
74 /* Get the thread descriptor definition. */
75 # include <nptl/descr.h>
79 # define LOCK_PREFIX /* nothing */
81 # define LOCK_PREFIX "lock;"
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; \
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" \
144 : "0" ((unsigned long int) __NR_arch_prctl), \
145 "D" ((unsigned long int) ARCH_SET_FS), \
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
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))); \
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" \
181 : "0" (0), "i" (offsetof (struct pthread, member))); \
182 else if (sizeof (__value) == 4) \
183 asm volatile ("movl %%fs:%P1,%0" \
185 : "i" (offsetof (struct pthread, member))); \
188 if (sizeof (__value) != 8) \
189 /* There should not be any value with a size other than 1, \
193 asm volatile ("movq %%fs:%P1,%q0" \
195 : "i" (offsetof (struct pthread, member))); \
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" \
206 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
208 else if (sizeof (__value) == 4) \
209 asm volatile ("movl %%fs:%P1(,%q2,4),%0" \
211 : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
214 if (sizeof (__value) != 8) \
215 /* There should not be any value with a size other than 1, \
219 asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \
221 : "i" (offsetof (struct pthread, member[0])), \
227 /* Loading addresses of objects on x86-64 needs to be treated special
228 when generating PIC code. */
230 # define IMM_MODE "nr"
232 # define IMM_MODE "ir"
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" : \
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))); \
248 if (sizeof (descr->member) != 8) \
249 /* There should not be any value with a size other than 1, \
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)" : \
264 "i" (offsetof (struct pthread, member[0])), \
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])), \
273 if (sizeof (descr->member[0]) != 8) \
274 /* There should not be any value with a size other than 1, \
278 asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \
279 : IMM_MODE ((unsigned long int) value), \
280 "i" (offsetof (struct pthread, member[0])), \
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" \
292 : "0" (__old), "r" (newval), \
293 "i" (offsetof (struct pthread, member))); \
295 /* Not necessary for other sizes in the moment. */ \
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))); \
307 /* Not necessary for other sizes in the moment. */ \
311 #define CALL_THREAD_FCT(descr) \
313 asm volatile ("movq %%fs:%P2, %%rdi\n\t" \
316 : "i" (offsetof (struct pthread, start_routine)), \
317 "i" (offsetof (struct pthread, arg)) \
318 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
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__ */