chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / nptl / sysdeps / x86_64 / tls.h
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.
4
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.
9
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.
14
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.  */
19
20 #ifndef _TLS_H
21 #define _TLS_H  1
22
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>
29 # include <sysdep.h>
30 # include <kernel-features.h>
31 # include <bits/wordsize.h>
32 # include <xmmintrin.h>
33
34
35 /* Type for the dtv.  */
36 typedef union dtv
37 {
38   size_t counter;
39   struct
40   {
41     void *val;
42     bool is_static;
43   } pointer;
44 } dtv_t;
45
46
47 typedef struct
48 {
49   void *tcb;            /* Pointer to the TCB.  Not necessarily the
50                            thread descriptor used by libpthread.  */
51   dtv_t *dtv;
52   void *self;           /* Pointer to the thread descriptor.  */
53   int multiple_threads;
54   int gscope_flag;
55   uintptr_t sysinfo;
56   uintptr_t stack_guard;
57   uintptr_t pointer_guard;
58   unsigned long int vgetcpu_cache[2];
59 # ifndef __ASSUME_PRIVATE_FUTEX
60   int private_futex;
61 # else
62   int __unused1;
63 # endif
64 # if __WORDSIZE == 64
65   int rtld_must_xmm_save;
66 # endif
67   /* Reservation of some values for the TM ABI.  */
68   void *__private_tm[5];
69 # if __WORDSIZE == 64
70   long int __unused2;
71   /* Have space for the post-AVX register size.  */
72   __m128 rtld_savespace_sse[8][4];
73
74   void *__padding[8];
75 # endif
76 } tcbhead_t;
77
78 #else /* __ASSEMBLER__ */
79 # include <tcb-offsets.h>
80 #endif
81
82
83 /* We require TLS support in the tools.  */
84 #ifndef HAVE_TLS_SUPPORT
85 # error "TLS support is required."
86 #endif
87
88 /* Alignment requirement for the stack.  */
89 #define STACK_ALIGN     16
90
91
92 #ifndef __ASSEMBLER__
93 /* Get system call information.  */
94 # include <sysdep.h>
95
96
97 /* Get the thread descriptor definition.  */
98 # include <nptl/descr.h>
99
100 #ifndef LOCK_PREFIX
101 # ifdef UP
102 #  define LOCK_PREFIX   /* nothing */
103 # else
104 #  define LOCK_PREFIX   "lock;"
105 # endif
106 #endif
107
108 /* This is the size of the initial TCB.  Can't be just sizeof (tcbhead_t),
109    because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
110    struct pthread even when not linked with -lpthread.  */
111 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
112
113 /* Alignment requirements for the initial TCB.  */
114 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
115
116 /* This is the size of the TCB.  */
117 # define TLS_TCB_SIZE sizeof (struct pthread)
118
119 /* Alignment requirements for the TCB.  */
120 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
121
122 /* The TCB can have any size and the memory following the address the
123    thread pointer points to is unspecified.  Allocate the TCB there.  */
124 # define TLS_TCB_AT_TP  1
125
126
127 /* Install the dtv pointer.  The pointer passed is to the element with
128    index -1 which contain the length.  */
129 # define INSTALL_DTV(descr, dtvp) \
130   ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
131
132 /* Install new dtv for current thread.  */
133 # define INSTALL_NEW_DTV(dtvp) \
134   ({ struct pthread *__pd;                                                    \
135      THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
136
137 /* Return dtv of given thread descriptor.  */
138 # define GET_DTV(descr) \
139   (((tcbhead_t *) (descr))->dtv)
140
141
142 /* Macros to load from and store into segment registers.  */
143 # define TLS_GET_FS() \
144   ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; })
145 # define TLS_SET_FS(val) \
146   __asm ("movl %0, %%fs" :: "q" (val))
147
148
149 /* Code to initially initialize the thread pointer.  This might need
150    special attention since 'errno' is not yet available and if the
151    operation can cause a failure 'errno' must not be touched.
152
153    We have to make the syscall for both uses of the macro since the
154    address might be (and probably is) different.  */
155 # define TLS_INIT_TP(thrdescr, secondcall) \
156   ({ void *_thrdescr = (thrdescr);                                            \
157      tcbhead_t *_head = _thrdescr;                                            \
158      int _result;                                                             \
159                                                                               \
160      _head->tcb = _thrdescr;                                                  \
161      /* For now the thread descriptor is at the same address.  */             \
162      _head->self = _thrdescr;                                                 \
163                                                                               \
164      /* It is a simple syscall to set the %fs value for the thread.  */       \
165      asm volatile ("syscall"                                                  \
166                    : "=a" (_result)                                           \
167                    : "0" ((unsigned long int) __NR_arch_prctl),               \
168                      "D" ((unsigned long int) ARCH_SET_FS),                   \
169                      "S" (_thrdescr)                                          \
170                    : "memory", "cc", "r11", "cx");                            \
171                                                                               \
172     _result ? "cannot set %fs base address for thread-local storage" : 0;     \
173   })
174
175
176 /* Return the address of the dtv for the current thread.  */
177 # define THREAD_DTV() \
178   ({ struct pthread *__pd;                                                    \
179      THREAD_GETMEM (__pd, header.dtv); })
180
181
182 /* Return the thread descriptor for the current thread.
183
184    The contained asm must *not* be marked volatile since otherwise
185    assignments like
186         pthread_descr self = thread_self();
187    do not get optimized away.  */
188 # define THREAD_SELF \
189   ({ struct pthread *__self;                                                  \
190      asm ("movq %%fs:%c1,%q0" : "=r" (__self)                                 \
191           : "i" (offsetof (struct pthread, header.self)));                    \
192      __self;})
193
194 /* Magic for libthread_db to know how to do THREAD_SELF.  */
195 # define DB_THREAD_SELF_INCLUDE  <sys/reg.h> /* For the FS constant.  */
196 # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS)
197
198 /* Read member of the thread descriptor directly.  */
199 # define THREAD_GETMEM(descr, member) \
200   ({ __typeof (descr->member) __value;                                        \
201      if (sizeof (__value) == 1)                                               \
202        asm volatile ("movb %%fs:%P2,%b0"                                      \
203                      : "=q" (__value)                                         \
204                      : "0" (0), "i" (offsetof (struct pthread, member)));     \
205      else if (sizeof (__value) == 4)                                          \
206        asm volatile ("movl %%fs:%P1,%0"                                       \
207                      : "=r" (__value)                                         \
208                      : "i" (offsetof (struct pthread, member)));              \
209      else                                                                     \
210        {                                                                      \
211          if (sizeof (__value) != 8)                                           \
212            /* There should not be any value with a size other than 1,         \
213               4 or 8.  */                                                     \
214            abort ();                                                          \
215                                                                               \
216          asm volatile ("movq %%fs:%P1,%q0"                                    \
217                        : "=r" (__value)                                       \
218                        : "i" (offsetof (struct pthread, member)));            \
219        }                                                                      \
220      __value; })
221
222
223 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
224 # define THREAD_GETMEM_NC(descr, member, idx) \
225   ({ __typeof (descr->member[0]) __value;                                     \
226      if (sizeof (__value) == 1)                                               \
227        asm volatile ("movb %%fs:%P2(%q3),%b0"                                 \
228                      : "=q" (__value)                                         \
229                      : "0" (0), "i" (offsetof (struct pthread, member[0])),   \
230                        "r" (idx));                                            \
231      else if (sizeof (__value) == 4)                                          \
232        asm volatile ("movl %%fs:%P1(,%q2,4),%0"                               \
233                      : "=r" (__value)                                         \
234                      : "i" (offsetof (struct pthread, member[0])), "r" (idx));\
235      else                                                                     \
236        {                                                                      \
237          if (sizeof (__value) != 8)                                           \
238            /* There should not be any value with a size other than 1,         \
239               4 or 8.  */                                                     \
240            abort ();                                                          \
241                                                                               \
242          asm volatile ("movq %%fs:%P1(,%q2,8),%q0"                            \
243                        : "=r" (__value)                                       \
244                        : "i" (offsetof (struct pthread, member[0])),          \
245                          "r" (idx));                                          \
246        }                                                                      \
247      __value; })
248
249
250 /* Loading addresses of objects on x86-64 needs to be treated special
251    when generating PIC code.  */
252 #ifdef __pic__
253 # define IMM_MODE "nr"
254 #else
255 # define IMM_MODE "ir"
256 #endif
257
258
259 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
260 # define THREAD_SETMEM(descr, member, value) \
261   ({ if (sizeof (descr->member) == 1)                                         \
262        asm volatile ("movb %b0,%%fs:%P1" :                                    \
263                      : "iq" (value),                                          \
264                        "i" (offsetof (struct pthread, member)));              \
265      else if (sizeof (descr->member) == 4)                                    \
266        asm volatile ("movl %0,%%fs:%P1" :                                     \
267                      : IMM_MODE (value),                                      \
268                        "i" (offsetof (struct pthread, member)));              \
269      else                                                                     \
270        {                                                                      \
271          if (sizeof (descr->member) != 8)                                     \
272            /* There should not be any value with a size other than 1,         \
273               4 or 8.  */                                                     \
274            abort ();                                                          \
275                                                                               \
276          asm volatile ("movq %q0,%%fs:%P1" :                                  \
277                        : IMM_MODE ((unsigned long int) value),                \
278                          "i" (offsetof (struct pthread, member)));            \
279        }})
280
281
282 /* Set member of the thread descriptor directly.  */
283 # define THREAD_SETMEM_NC(descr, member, idx, value) \
284   ({ if (sizeof (descr->member[0]) == 1)                                      \
285        asm volatile ("movb %b0,%%fs:%P1(%q2)" :                               \
286                      : "iq" (value),                                          \
287                        "i" (offsetof (struct pthread, member[0])),            \
288                        "r" (idx));                                            \
289      else if (sizeof (descr->member[0]) == 4)                                 \
290        asm volatile ("movl %0,%%fs:%P1(,%q2,4)" :                             \
291                      : IMM_MODE (value),                                      \
292                        "i" (offsetof (struct pthread, member[0])),            \
293                        "r" (idx));                                            \
294      else                                                                     \
295        {                                                                      \
296          if (sizeof (descr->member[0]) != 8)                                  \
297            /* There should not be any value with a size other than 1,         \
298               4 or 8.  */                                                     \
299            abort ();                                                          \
300                                                                               \
301          asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" :                          \
302                        : IMM_MODE ((unsigned long int) value),                \
303                          "i" (offsetof (struct pthread, member[0])),          \
304                          "r" (idx));                                          \
305        }})
306
307
308 /* Atomic compare and exchange on TLS, returning old value.  */
309 # define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
310   ({ __typeof (descr->member) __ret;                                          \
311      __typeof (oldval) __old = (oldval);                                      \
312      if (sizeof (descr->member) == 4)                                         \
313        asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3"                      \
314                      : "=a" (__ret)                                           \
315                      : "0" (__old), "r" (newval),                             \
316                        "i" (offsetof (struct pthread, member)));              \
317      else                                                                     \
318        /* Not necessary for other sizes in the moment.  */                    \
319        abort ();                                                              \
320      __ret; })
321
322
323 /* Atomic logical and.  */
324 # define THREAD_ATOMIC_AND(descr, member, val) \
325   (void) ({ if (sizeof ((descr)->member) == 4)                                \
326               asm volatile (LOCK_PREFIX "andl %1, %%fs:%P0"                   \
327                             :: "i" (offsetof (struct pthread, member)),       \
328                                "ir" (val));                                   \
329             else                                                              \
330               /* Not necessary for other sizes in the moment.  */             \
331               abort (); })
332
333
334 /* Atomic set bit.  */
335 # define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
336   (void) ({ if (sizeof ((descr)->member) == 4)                                \
337               asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0"                    \
338                             :: "i" (offsetof (struct pthread, member)),       \
339                                "ir" (1 << (bit)));                            \
340             else                                                              \
341               /* Not necessary for other sizes in the moment.  */             \
342               abort (); })
343
344
345 # define CALL_THREAD_FCT(descr) \
346   ({ void *__res;                                                             \
347      asm volatile ("movq %%fs:%P2, %%rdi\n\t"                                 \
348                    "callq *%%fs:%P1"                                          \
349                    : "=a" (__res)                                             \
350                    : "i" (offsetof (struct pthread, start_routine)),          \
351                      "i" (offsetof (struct pthread, arg))                     \
352                    : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11",        \
353                      "memory", "cc");                                         \
354      __res; })
355
356
357 /* Set the stack guard field in TCB head.  */
358 # define THREAD_SET_STACK_GUARD(value) \
359     THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
360 # define THREAD_COPY_STACK_GUARD(descr) \
361     ((descr)->header.stack_guard                                              \
362      = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
363
364
365 /* Set the pointer guard field in the TCB head.  */
366 # define THREAD_SET_POINTER_GUARD(value) \
367   THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
368 # define THREAD_COPY_POINTER_GUARD(descr) \
369   ((descr)->header.pointer_guard                                              \
370    = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
371
372
373 /* Get and set the global scope generation counter in the TCB head.  */
374 # define THREAD_GSCOPE_FLAG_UNUSED 0
375 # define THREAD_GSCOPE_FLAG_USED   1
376 # define THREAD_GSCOPE_FLAG_WAIT   2
377 # define THREAD_GSCOPE_RESET_FLAG() \
378   do                                                                          \
379     { int __res;                                                              \
380       asm volatile ("xchgl %0, %%fs:%P1"                                      \
381                     : "=r" (__res)                                            \
382                     : "i" (offsetof (struct pthread, header.gscope_flag)),    \
383                       "0" (THREAD_GSCOPE_FLAG_UNUSED));                       \
384       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                   \
385         lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);    \
386     }                                                                         \
387   while (0)
388 # define THREAD_GSCOPE_SET_FLAG() \
389   THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
390 # define THREAD_GSCOPE_WAIT() \
391   GL(dl_wait_lookup_done) ()
392
393
394 # ifdef SHARED
395 /* Defined in dl-trampoline.S.  */
396 extern void _dl_x86_64_save_sse (void);
397 extern void _dl_x86_64_restore_sse (void);
398
399 # define RTLD_CHECK_FOREIGN_CALL \
400   (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
401
402 /* NB: Don't use the xchg operation because that would imply a lock
403    prefix which is expensive and unnecessary.  The cache line is also
404    not contested at all.  */
405 #  define RTLD_ENABLE_FOREIGN_CALL \
406   int old_rtld_must_xmm_save = THREAD_GETMEM (THREAD_SELF,                    \
407                                               header.rtld_must_xmm_save);     \
408   THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 1)
409
410 #  define RTLD_PREPARE_FOREIGN_CALL \
411   do if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save))              \
412     {                                                                         \
413       _dl_x86_64_save_sse ();                                                 \
414       THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0);              \
415     }                                                                         \
416   while (0)
417
418 #  define RTLD_FINALIZE_FOREIGN_CALL \
419   do {                                                                        \
420     if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) == 0)          \
421       _dl_x86_64_restore_sse ();                                              \
422     THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save,                    \
423                    old_rtld_must_xmm_save);                                   \
424   } while (0)
425 # endif
426
427
428 #endif /* __ASSEMBLER__ */
429
430 #endif  /* tls.h */