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