]> 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 #ifndef __ASSEMBLER__
23 //l4/# 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 //l4/# 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 //l4/# include <sysdep.h>
97
98
99 /* Get the thread descriptor definition.  */
100 # include <descr.h>
101 # include <l4/sys/utcb.h>
102 # include <l4/sys/segment.h>
103
104 #ifndef LOCK_PREFIX
105 # ifdef UP
106 #  define LOCK_PREFIX   /* nothing */
107 # else
108 #  define LOCK_PREFIX   "lock;"
109 # endif
110 #endif
111
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)
116
117 /* Alignment requirements for the initial TCB.  */
118 # define TLS_INIT_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
119
120 /* This is the size of the TCB.  */
121 # define TLS_TCB_SIZE sizeof (struct _pthread_descr_struct)
122
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
130
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
134
135
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
140
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)); })
148
149 /* Return dtv of given thread descriptor.  */
150 # define GET_DTV(descr) \
151   (((tcbhead_t *) (descr))->dtv)
152
153
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))
159
160
161 static inline char const *TLS_INIT_TP(void *thrdescr, int secondcall)
162 {
163   (void)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)
168     return "ERROR";
169
170   return NULL;
171 }
172 #if 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.
176
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;                                            \
182      int _result;                                                             \
183                                                                               \
184      _head->tcb = _thrdescr;                                                  \
185      /* For now the thread descriptor is at the same address.  */             \
186      _head->self = _thrdescr;                                                 \
187                                                                               \
188      /* It is a simple syscall to set the %fs value for the thread.  */       \
189      __asm__ __volatile__ ("syscall"                                                  \
190                    : "=a" (_result)                                           \
191                    : "0" ((unsigned long int) __NR_arch_prctl),               \
192                      "D" ((unsigned long int) ARCH_SET_FS),                   \
193                      "S" (_thrdescr)                                          \
194                    : "memory", "cc", "r11", "cx");                            \
195                                                                               \
196     _result ? "cannot set %fs base address for thread-local storage" : 0;     \
197   })
198 #endif
199
200
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); })
205
206 # define THREAD_DTV() \
207   ({ struct _pthread_descr_struct *__pd;                                                      \
208      THREAD_GETMEM (__pd, p_header.data.dtvp); })
209
210
211 /* Return the thread descriptor for the current thread.
212
213    The contained asm must *not* be marked __volatile__ since otherwise
214    assignments like
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)));                    \
221      __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)));               \
226      __self;})
227
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)
231
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"                                      \
237                      : "=q" (__value)                                         \
238                      : "0" (0), "i" (offsetof (struct _pthread_descr_struct, member)));     \
239      else if (sizeof (__value) == 4)                                          \
240        __asm__ __volatile__ ("movl %%fs:%P1,%0"                                       \
241                      : "=r" (__value)                                         \
242                      : "i" (offsetof (struct _pthread_descr_struct, member)));        \
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,%q0"                                    \
251                        : "=r" (__value)                                       \
252                        : "i" (offsetof (struct _pthread_descr_struct, member)));              \
253        }                                                                      \
254      __value; })
255
256
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"                                 \
262                      : "=q" (__value)                                         \
263                      : "0" (0), "i" (offsetof (struct _pthread_descr_struct, member[0])),   \
264                        "r" (idx));                                            \
265      else if (sizeof (__value) == 4)                                          \
266        __asm__ __volatile__ ("movl %%fs:%P1(,%q2,4),%0"                               \
267                      : "=r" (__value)                                         \
268                      : "i" (offsetof (struct _pthread_descr_struct, member[0])), "r" (idx));\
269      else                                                                     \
270        {                                                                      \
271          if (sizeof (__value) != 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 %%fs:%P1(,%q2,8),%q0"                            \
277                        : "=r" (__value)                                       \
278                        : "i" (offsetof (struct _pthread_descr_struct, member[0])),            \
279                          "r" (idx));                                          \
280        }                                                                      \
281      __value; })
282
283
284 /* Loading addresses of objects on x86-64 needs to be treated special
285    when generating PIC code.  */
286 #ifdef __pic__
287 # define IMM_MODE "nr"
288 #else
289 # define IMM_MODE "ir"
290 #endif
291
292
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" :                                    \
297                      : "iq" (value),                                          \
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)));        \
303      else                                                                     \
304        {                                                                      \
305          if (sizeof (descr->member) != 8)                                     \
306            /* There should not be any value with a size other than 1,         \
307               4 or 8.  */                                                     \
308            abort ();                                                          \
309                                                                               \
310          __asm__ __volatile__ ("movq %q0,%%fs:%P1" :                                  \
311                        : IMM_MODE ((unsigned long int) value),                \
312                          "i" (offsetof (struct _pthread_descr_struct, member)));              \
313        }})
314
315
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)" :                               \
320                      : "iq" (value),                                          \
321                        "i" (offsetof (struct _pthread_descr_struct, member[0])),              \
322                        "r" (idx));                                            \
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])),              \
327                        "r" (idx));                                            \
328      else                                                                     \
329        {                                                                      \
330          if (sizeof (descr->member[0]) != 8)                                  \
331            /* There should not be any value with a size other than 1,         \
332               4 or 8.  */                                                     \
333            abort ();                                                          \
334                                                                               \
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])),            \
338                          "r" (idx));                                          \
339        }})
340
341
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"                      \
348                      : "=a" (__ret)                                           \
349                      : "0" (__old), "r" (newval),                             \
350                        "i" (offsetof (struct pthread, member)));              \
351      else                                                                     \
352        /* Not necessary for other sizes in the moment.  */                    \
353        abort ();                                                              \
354      __ret; })
355
356
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)),       \
362                                "ir" (val));                                   \
363             else                                                              \
364               /* Not necessary for other sizes in the moment.  */             \
365               abort (); })
366
367
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)));                            \
374             else                                                              \
375               /* Not necessary for other sizes in the moment.  */             \
376               abort (); })
377
378
379 # define CALL_THREAD_FCT(descr) \
380   ({ void *__res;                                                             \
381      __asm__ __volatile__ ("movq %%fs:%P2, %%rdi\n\t"                                 \
382                    "callq *%%fs:%P1"                                          \
383                    : "=a" (__res)                                             \
384                    : "i" (offsetof (struct pthread, start_routine)),          \
385                      "i" (offsetof (struct pthread, arg))                     \
386                    : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11",        \
387                      "memory", "cc");                                         \
388      __res; })
389
390
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))
397
398
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))
405
406
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() \
412   do                                                                          \
413     { int __res;                                                              \
414       __asm__ __volatile__ ("xchgl %0, %%fs:%P1"                                      \
415                     : "=r" (__res)                                            \
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);    \
420     }                                                                         \
421   while (0)
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) ()
426
427
428 # ifdef SHARED
429 /* Defined in dl-trampoline.S.  */
430 extern void _dl_x86_64_save_sse (void);
431 extern void _dl_x86_64_restore_sse (void);
432
433 # define RTLD_CHECK_FOREIGN_CALL \
434   (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save) != 0)
435
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)
443
444 #  define RTLD_PREPARE_FOREIGN_CALL \
445   do if (THREAD_GETMEM (THREAD_SELF, header.rtld_must_xmm_save))              \
446     {                                                                         \
447       _dl_x86_64_save_sse ();                                                 \
448       THREAD_SETMEM (THREAD_SELF, header.rtld_must_xmm_save, 0);              \
449     }                                                                         \
450   while (0)
451
452 #  define RTLD_FINALIZE_FOREIGN_CALL \
453   do {                                                                        \
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);                                   \
458   } while (0)
459 # endif
460
461
462 #endif /* __ASSEMBLER__ */
463 #endif  /* tls.h */