1 /* Definition for thread-local data handling. nptl/x86_64 version.
2 Copyright (C) 2002-2007, 2008, 2009 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
23 //l4/# include <asm/prctl.h> /* For ARCH_SET_FS. */
28 //l4/# include <sysdep.h>
29 # include <bits/kernel-features.h>
30 # include <bits/wordsize.h>
31 # include <xmmintrin.h>
34 /* Type for the dtv. */
48 void *tcb; /* Pointer to the TCB. Not necessarily the
49 thread descriptor used by libpthread. */
51 void *self; /* Pointer to the thread descriptor. */
55 uintptr_t stack_guard;
56 uintptr_t pointer_guard;
57 unsigned long int vgetcpu_cache[2];
58 # ifndef __ASSUME_PRIVATE_FUTEX
64 int rtld_must_xmm_save;
66 /* Reservation of some values for the TM ABI. */
67 void *__private_tm[5];
70 /* Have space for the post-AVX register size. */
71 __m128 rtld_savespace_sse[8][4];
77 #else /* __ASSEMBLER__ */
78 # include <tcb-offsets.h>
82 /* We require TLS support in the tools. */
83 #define HAVE_TLS_SUPPORT 1
84 #define HAVE___THREAD 1
85 #define HAVE_TLS_MODEL_ATTRIBUTE 1
87 /* Signal that TLS support is available. */
90 /* Alignment requirement for the stack. */
91 #define STACK_ALIGN 16
95 /* Get system call information. */
96 //l4/# include <sysdep.h>
99 /* Get the thread descriptor definition. */
101 # include <l4/sys/utcb.h>
102 # include <l4/sys/segment.h>
106 # define LOCK_PREFIX /* nothing */
108 # define LOCK_PREFIX "lock;"
112 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
113 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
114 struct pthread even when not linked with -lpthread. */
115 # define TLS_INIT_TCB_SIZE sizeof (struct _pthread_descr_struct)
117 /* Alignment requirements for the initial TCB. */
118 # define TLS_INIT_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
120 /* This is the size of the TCB. */
121 # define TLS_TCB_SIZE sizeof (struct _pthread_descr_struct)
123 /* Alignment requirements for the TCB. */
124 //# define TLS_TCB_ALIGN __alignof__ (struct pthread)
125 // Normally the above would be correct But we have to store post-AVX
126 // vector registers in the TCB and we want the storage to be aligned.
127 // unfortunately there isn't yet a type for these values and hence no
128 // 32-byte alignment requirement. Make this explicit, for now.
129 # define TLS_TCB_ALIGN 32
131 /* The TCB can have any size and the memory following the address the
132 thread pointer points to is unspecified. Allocate the TCB there. */
133 # define TLS_TCB_AT_TP 1
136 /* Install the dtv pointer. The pointer passed is to the element with
137 index -1 which contain the length. */
138 # define INSTALL_DTV(descr, dtvp) \
139 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
141 /* Install new dtv for current thread. */
142 # define INSTALL_NEW_DTV_ORIG(dtvp) \
143 ({ struct pthread *__pd; \
144 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
145 # define INSTALL_NEW_DTV(dtvpx) \
146 ({ struct _pthread_descr_struct *__pd; \
147 THREAD_SETMEM (__pd, p_header.data.dtvp, (dtvpx)); })
149 /* Return dtv of given thread descriptor. */
150 # define GET_DTV(descr) \
151 (((tcbhead_t *) (descr))->dtv)
154 /* Macros to load from and store into segment registers. */
155 # define TLS_GET_FS() \
156 ({ int __seg; __asm__ ("movl %%fs, %0" : "=q" (__seg)); __seg; })
157 # define TLS_SET_FS(val) \
158 __asm__ ("movl %0, %%fs" :: "q" (val))
161 static inline char const *TLS_INIT_TP(void *thrdescr, int secondcall)
164 tcbhead_t *_head = (tcbhead_t *)thrdescr;
165 _head->tcb = thrdescr;
166 _head->self = thrdescr;
167 if (fiasco_amd64_set_fs(L4_INVALID_CAP, (l4_umword_t)thrdescr, l4_utcb()) < 0)
173 /* Code to initially initialize the thread pointer. This might need
174 special attention since 'errno' is not yet available and if the
175 operation can cause a failure 'errno' must not be touched.
177 We have to make the syscall for both uses of the macro since the
178 address might be (and probably is) different. */
179 # define TLS_INIT_TP(thrdescr, secondcall) \
180 ({ void *_thrdescr = (thrdescr); \
181 tcbhead_t *_head = _thrdescr; \
184 _head->tcb = _thrdescr; \
185 /* For now the thread descriptor is at the same address. */ \
186 _head->self = _thrdescr; \
188 /* It is a simple syscall to set the %fs value for the thread. */ \
189 __asm__ __volatile__ ("syscall" \
191 : "0" ((unsigned long int) __NR_arch_prctl), \
192 "D" ((unsigned long int) ARCH_SET_FS), \
194 : "memory", "cc", "r11", "cx"); \
196 _result ? "cannot set %fs base address for thread-local storage" : 0; \
201 /* Return the address of the dtv for the current thread. */
202 # define THREAD_DTV_ORIG() \
203 ({ struct pthread *__pd; \
204 THREAD_GETMEM (__pd, header.dtv); })
206 # define THREAD_DTV() \
207 ({ struct _pthread_descr_struct *__pd; \
208 THREAD_GETMEM (__pd, p_header.data.dtvp); })
211 /* Return the thread descriptor for the current thread.
213 The contained asm must *not* be marked __volatile__ since otherwise
215 pthread_descr self = thread_self();
216 do not get optimized away. */
217 # define THREAD_SELF_ORIG \
218 ({ struct pthread *__self; \
219 __asm__ ("movq %%fs:%c1,%q0" : "=r" (__self) \
220 : "i" (offsetof (struct pthread, header.self))); \
222 # define THREAD_SELF \
223 ({ struct _pthread_descr_struct *__self; \
224 __asm__ ("movq %%fs:%c1,%q0" : "=r" (__self) \
225 : "i" (offsetof (struct _pthread_descr_struct, p_header.data.self))); \
228 /* Magic for libthread_db to know how to do THREAD_SELF. */
229 # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */
230 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
232 /* Read member of the thread descriptor directly. */
233 # define THREAD_GETMEM(descr, member) \
234 ({ __typeof (descr->member) __value; \
235 if (sizeof (__value) == 1) \
236 __asm__ __volatile__ ("movb %%fs:%P2,%b0" \
238 : "0" (0), "i" (offsetof (struct _pthread_descr_struct, member))); \
239 else if (sizeof (__value) == 4) \
240 __asm__ __volatile__ ("movl %%fs:%P1,%0" \
242 : "i" (offsetof (struct _pthread_descr_struct, member))); \
245 if (sizeof (__value) != 8) \
246 /* There should not be any value with a size other than 1, \
250 __asm__ __volatile__ ("movq %%fs:%P1,%q0" \
252 : "i" (offsetof (struct _pthread_descr_struct, member))); \
257 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
258 # define THREAD_GETMEM_NC(descr, member, idx) \
259 ({ __typeof (descr->member[0]) __value; \
260 if (sizeof (__value) == 1) \
261 __asm__ __volatile__ ("movb %%fs:%P2(%q3),%b0" \
263 : "0" (0), "i" (offsetof (struct _pthread_descr_struct, member[0])), \
265 else if (sizeof (__value) == 4) \
266 __asm__ __volatile__ ("movl %%fs:%P1(,%q2,4),%0" \
268 : "i" (offsetof (struct _pthread_descr_struct, member[0])), "r" (idx));\
271 if (sizeof (__value) != 8) \
272 /* There should not be any value with a size other than 1, \
276 __asm__ __volatile__ ("movq %%fs:%P1(,%q2,8),%q0" \
278 : "i" (offsetof (struct _pthread_descr_struct, member[0])), \
284 /* Loading addresses of objects on x86-64 needs to be treated special
285 when generating PIC code. */
287 # define IMM_MODE "nr"
289 # define IMM_MODE "ir"
293 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
294 # define THREAD_SETMEM(descr, member, value) \
295 ({ if (sizeof (descr->member) == 1) \
296 __asm__ __volatile__ ("movb %b0,%%fs:%P1" : \
298 "i" (offsetof (struct _pthread_descr_struct, member))); \
299 else if (sizeof (descr->member) == 4) \
300 __asm__ __volatile__ ("movl %0,%%fs:%P1" : \
301 : IMM_MODE (value), \
302 "i" (offsetof (struct _pthread_descr_struct, member))); \
305 if (sizeof (descr->member) != 8) \
306 /* There should not be any value with a size other than 1, \
310 __asm__ __volatile__ ("movq %q0,%%fs:%P1" : \
311 : IMM_MODE ((unsigned long int) value), \
312 "i" (offsetof (struct _pthread_descr_struct, member))); \
316 /* Set member of the thread descriptor directly. */
317 # define THREAD_SETMEM_NC(descr, member, idx, value) \
318 ({ if (sizeof (descr->member[0]) == 1) \
319 __asm__ __volatile__ ("movb %b0,%%fs:%P1(%q2)" : \
321 "i" (offsetof (struct _pthread_descr_struct, member[0])), \
323 else if (sizeof (descr->member[0]) == 4) \
324 __asm__ __volatile__ ("movl %0,%%fs:%P1(,%q2,4)" : \
325 : IMM_MODE (value), \
326 "i" (offsetof (struct _pthread_descr_struct, member[0])), \
330 if (sizeof (descr->member[0]) != 8) \
331 /* There should not be any value with a size other than 1, \
335 __asm__ __volatile__ ("movq %q0,%%fs:%P1(,%q2,8)" : \
336 : IMM_MODE ((unsigned long int) value), \
337 "i" (offsetof (struct _pthread_descr_struct, member[0])), \
342 /* Atomic compare and exchange on TLS, returning old value. */
343 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
344 ({ __typeof (descr->member) __ret; \
345 __typeof (oldval) __old = (oldval); \
346 if (sizeof (descr->member) == 4) \
347 __asm__ __volatile__ (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \
349 : "0" (__old), "r" (newval), \
350 "i" (offsetof (struct pthread, member))); \
352 /* Not necessary for other sizes in the moment. */ \
357 /* Atomic logical and. */
358 # define THREAD_ATOMIC_AND(descr, member, val) \
359 (void) ({ if (sizeof ((descr)->member) == 4) \
360 __asm__ __volatile__ (LOCK_PREFIX "andl %1, %%fs:%P0" \
361 :: "i" (offsetof (struct pthread, member)), \
364 /* Not necessary for other sizes in the moment. */ \
368 /* Atomic set bit. */
369 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
370 (void) ({ if (sizeof ((descr)->member) == 4) \
371 __asm__ __volatile__ (LOCK_PREFIX "orl %1, %%fs:%P0" \
372 :: "i" (offsetof (struct pthread, member)), \
373 "ir" (1 << (bit))); \
375 /* Not necessary for other sizes in the moment. */ \
379 # define CALL_THREAD_FCT(descr) \
381 __asm__ __volatile__ ("movq %%fs:%P2, %%rdi\n\t" \
384 : "i" (offsetof (struct pthread, start_routine)), \
385 "i" (offsetof (struct pthread, arg)) \
386 : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \
391 /* Set the stack guard field in TCB head. */
392 # define THREAD_SET_STACK_GUARD(value) \
393 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
394 # define THREAD_COPY_STACK_GUARD(descr) \
395 ((descr)->header.stack_guard \
396 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
399 /* Set the pointer guard field in the TCB head. */
400 # define THREAD_SET_POINTER_GUARD(value) \
401 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
402 # define THREAD_COPY_POINTER_GUARD(descr) \
403 ((descr)->header.pointer_guard \
404 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
407 /* Get and set the global scope generation counter in the TCB head. */
408 # define THREAD_GSCOPE_FLAG_UNUSED 0
409 # define THREAD_GSCOPE_FLAG_USED 1
410 # define THREAD_GSCOPE_FLAG_WAIT 2
411 # define THREAD_GSCOPE_RESET_FLAG() \
414 __asm__ __volatile__ ("xchgl %0, %%fs:%P1" \
416 : "i" (offsetof (struct pthread, header.gscope_flag)), \
417 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
418 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
419 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
422 # define THREAD_GSCOPE_SET_FLAG() \
423 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
424 # define THREAD_GSCOPE_WAIT() \
425 GL(dl_wait_lookup_done) ()
429 /* Defined in dl-trampoline.S. */
430 extern void _dl_x86_64_save_sse (void);
431 extern void _dl_x86_64_restore_sse (void);
433 # define RTLD_CHECK_FOREIGN_CALL \
434 (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
436 /* NB: Don't use the xchg operation because that would imply a lock
437 prefix which is expensive and unnecessary. The cache line is also
438 not contested at all. */
439 # define RTLD_ENABLE_FOREIGN_CALL \
440 int old_rtld_must_xmm_save = THREAD_GETMEM (THREAD_SELF, \
441 header.rtld_must_xmm_save); \
442 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 1)
444 # define RTLD_PREPARE_FOREIGN_CALL \
445 do if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save)) \
447 _dl_x86_64_save_sse (); \
448 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0); \
452 # define RTLD_FINALIZE_FOREIGN_CALL \
454 if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) == 0) \
455 _dl_x86_64_restore_sse (); \
456 THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, \
457 old_rtld_must_xmm_save); \
462 #endif /* __ASSEMBLER__ */