OSDN Git Service

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