]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libpthread/nptl/sysdeps/i386/tls.h
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libpthread / nptl / sysdeps / i386 / tls.h
1 /* Definition for thread-local data handling.  nptl/i386 version.
2    Copyright (C) 2002-2007, 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 <stdbool.h>
24 # include <stddef.h>
25 # include <stdint.h>
26 # include <stdlib.h>
27 # include <list.h>
28 # include <sysdep.h>
29 # include <bits/kernel-features.h>
30
31
32 /* Type for the dtv.  */
33 typedef union dtv
34 {
35   size_t counter;
36   struct
37   {
38     void *val;
39     bool is_static;
40   } pointer;
41 } dtv_t;
42
43
44 typedef struct
45 {
46   void *tcb;            /* Pointer to the TCB.  Not necessarily the
47                            thread descriptor used by libpthread.  */
48   dtv_t *dtv;
49   void *self;           /* Pointer to the thread descriptor.  */
50   int multiple_threads;
51   uintptr_t sysinfo;
52   uintptr_t stack_guard;
53   uintptr_t pointer_guard;
54   int gscope_flag;
55 #ifndef __ASSUME_PRIVATE_FUTEX
56   int private_futex;
57 #else
58   int __unused1;
59 #endif
60   /* Reservation of some values for the TM ABI.  */
61   void *__private_tm[5];
62 } tcbhead_t;
63
64 # define TLS_MULTIPLE_THREADS_IN_TCB 1
65
66 #else /* __ASSEMBLER__ */
67 # include <tcb-offsets.h>
68 #endif
69
70
71 /* We require TLS support in the tools.  */
72 #define HAVE_TLS_SUPPORT
73 #define HAVE___THREAD 1
74 #define HAVE_TLS_MODEL_ATTRIBUTE 1
75
76 /* Signal that TLS support is available.  */
77 #define USE_TLS        1
78
79
80 /* Alignment requirement for the stack.  For IA-32 this is governed by
81    the SSE memory functions.  */
82 #define STACK_ALIGN     16
83
84 #ifndef __ASSEMBLER__
85 /* Get system call information.  */
86 # include <sysdep.h>
87
88 /* The old way: using LDT.  */
89
90 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls.  */
91 struct user_desc
92 {
93   unsigned int entry_number;
94   unsigned long int base_addr;
95   unsigned int limit;
96   unsigned int seg_32bit:1;
97   unsigned int contents:2;
98   unsigned int read_exec_only:1;
99   unsigned int limit_in_pages:1;
100   unsigned int seg_not_present:1;
101   unsigned int useable:1;
102   unsigned int empty:25;
103 };
104
105 /* Initializing bit fields is slow.  We speed it up by using a union.  */
106 union user_desc_init
107 {
108   struct user_desc desc;
109   unsigned int vals[4];
110 };
111
112
113 /* Get the thread descriptor definition.  */
114 # include <descr.h>
115
116 /* This is the size of the initial TCB.  Can't be just sizeof (tcbhead_t),
117    because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
118    struct pthread even when not linked with -lpthread.  */
119 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
120
121 /* Alignment requirements for the initial TCB.  */
122 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
123
124 /* This is the size of the TCB.  */
125 # define TLS_TCB_SIZE sizeof (struct pthread)
126
127 /* Alignment requirements for the TCB.  */
128 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
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 #define THREAD_SELF_SYSINFO     THREAD_GETMEM (THREAD_SELF, header.sysinfo)
150 #define THREAD_SYSINFO(pd)      ((pd)->header.sysinfo)
151
152 /* Macros to load from and store into segment registers.  */
153 # ifndef TLS_GET_GS
154 #  define TLS_GET_GS() \
155   ({ int __seg; __asm__ ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
156 # endif
157 # ifndef TLS_SET_GS
158 #  define TLS_SET_GS(val) \
159   __asm__ ("movw %w0, %%gs" :: "q" (val))
160 # endif
161
162
163 # ifndef __NR_set_thread_area
164 #  define __NR_set_thread_area 243
165 # endif
166 # ifndef TLS_FLAG_WRITABLE
167 #  define TLS_FLAG_WRITABLE             0x00000001
168 # endif
169
170 // XXX Enable for the real world.
171 #if 0
172 # ifndef __ASSUME_SET_THREAD_AREA
173 #  error "we need set_thread_area"
174 # endif
175 #endif
176
177 # ifdef __PIC__
178 #  define TLS_EBX_ARG "r"
179 #  define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
180 # else
181 #  define TLS_EBX_ARG "b"
182 #  define TLS_LOAD_EBX
183 # endif
184
185 #if defined NEED_DL_SYSINFO
186 # define INIT_SYSINFO \
187   _head->sysinfo = GLRO(dl_sysinfo)
188 #else
189 # define INIT_SYSINFO
190 #endif
191
192 #ifndef LOCK_PREFIX
193 # ifdef UP
194 #  define LOCK_PREFIX  /* nothing */
195 # else
196 #  define LOCK_PREFIX "lock;"
197 # endif
198 #endif
199
200 /* Code to initially initialize the thread pointer.  This might need
201    special attention since 'errno' is not yet available and if the
202    operation can cause a failure 'errno' must not be touched.  */
203 # define TLS_INIT_TP(thrdescr, secondcall) \
204   ({ void *_thrdescr = (thrdescr);                                            \
205      tcbhead_t *_head = _thrdescr;                                            \
206      union user_desc_init _segdescr;                                          \
207      int _result;                                                             \
208                                                                               \
209      _head->tcb = _thrdescr;                                                  \
210      /* For now the thread descriptor is at the same address.  */             \
211      _head->self = _thrdescr;                                                 \
212      /* New syscall handling support.  */                                     \
213      INIT_SYSINFO;                                                            \
214                                                                               \
215      /* The 'entry_number' field.  Let the kernel pick a value.  */           \
216      if (secondcall)                                                          \
217        _segdescr.vals[0] = TLS_GET_GS () >> 3;                                \
218      else                                                                     \
219        _segdescr.vals[0] = -1;                                                \
220      /* The 'base_addr' field.  Pointer to the TCB.  */                       \
221      _segdescr.vals[1] = (unsigned long int) _thrdescr;                       \
222      /* The 'limit' field.  We use 4GB which is 0xfffff pages.  */            \
223      _segdescr.vals[2] = 0xfffff;                                             \
224      /* Collapsed value of the bitfield:                                      \
225           .seg_32bit = 1                                                      \
226           .contents = 0                                                       \
227           .read_exec_only = 0                                                 \
228           .limit_in_pages = 1                                                 \
229           .seg_not_present = 0                                                \
230           .useable = 1 */                                                     \
231      _segdescr.vals[3] = 0x51;                                                \
232                                                                               \
233      /* Install the TLS.  */                                                  \
234      __asm__ __volatile__ (TLS_LOAD_EBX                                           \
235                    "int $0x80\n\t"                                            \
236                    TLS_LOAD_EBX                                               \
237                    : "=a" (_result), "=m" (_segdescr.desc.entry_number)       \
238                    : "0" (__NR_set_thread_area),                              \
239                      TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc));    \
240                                                                               \
241      if (_result == 0)                                                        \
242        /* We know the index in the GDT, now load the segment register.        \
243           The use of the GDT is described by the value 3 in the lower         \
244           three bits of the segment descriptor value.                         \
245                                                                               \
246           Note that we have to do this even if the numeric value of           \
247           the descriptor does not change.  Loading the segment register       \
248           causes the segment information from the GDT to be loaded            \
249           which is necessary since we have changed it.   */                   \
250        TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3);                      \
251                                                                               \
252      _result == 0 ? NULL                                                      \
253      : "set_thread_area failed when setting up thread-local storage\n"; })
254
255
256 /* Return the address of the dtv for the current thread.  */
257 # define THREAD_DTV() \
258   ({ struct pthread *__pd;                                                    \
259      THREAD_GETMEM (__pd, header.dtv); })
260
261
262 /* Return the thread descriptor for the current thread.
263
264    The contained asm must *not* be marked __volatile__ since otherwise
265    assignments like
266         pthread_descr self = thread_self();
267    do not get optimized away.  */
268 # define THREAD_SELF \
269   ({ struct pthread *__self;                                                  \
270      __asm__ ("movl %%gs:%c1,%0" : "=r" (__self)                                    \
271           : "i" (offsetof (struct pthread, header.self)));                    \
272      __self;})
273
274 /* Magic for libthread_db to know how to do THREAD_SELF.  */
275 # define DB_THREAD_SELF \
276   REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
277   REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
278
279
280 /* Read member of the thread descriptor directly.  */
281 # define THREAD_GETMEM(descr, member) \
282   ({ __typeof (descr->member) __value;                                        \
283      if (sizeof (__value) == 1)                                               \
284        __asm__ __volatile__ ("movb %%gs:%P2,%b0"                                    \
285                      : "=q" (__value)                                         \
286                      : "0" (0), "i" (offsetof (struct pthread, member)));     \
287      else if (sizeof (__value) == 4)                                          \
288        __asm__ __volatile__ ("movl %%gs:%P1,%0"                                     \
289                      : "=r" (__value)                                         \
290                      : "i" (offsetof (struct pthread, member)));              \
291      else                                                                     \
292        {                                                                      \
293          if (sizeof (__value) != 8)                                           \
294            /* There should not be any value with a size other than 1,         \
295               4 or 8.  */                                                     \
296            abort ();                                                          \
297                                                                               \
298          __asm__ __volatile__ ("movl %%gs:%P1,%%eax\n\t"                              \
299                        "movl %%gs:%P2,%%edx"                                  \
300                        : "=A" (__value)                                       \
301                        : "i" (offsetof (struct pthread, member)),             \
302                          "i" (offsetof (struct pthread, member) + 4));        \
303        }                                                                      \
304      __value; })
305
306
307 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
308 # define THREAD_GETMEM_NC(descr, member, idx) \
309   ({ __typeof (descr->member[0]) __value;                                     \
310      if (sizeof (__value) == 1)                                               \
311        __asm__ __volatile__ ("movb %%gs:%P2(%3),%b0"                                  \
312                      : "=q" (__value)                                         \
313                      : "0" (0), "i" (offsetof (struct pthread, member[0])),   \
314                      "r" (idx));                                              \
315      else if (sizeof (__value) == 4)                                          \
316        __asm__ __volatile__ ("movl %%gs:%P1(,%2,4),%0"                                \
317                      : "=r" (__value)                                         \
318                      : "i" (offsetof (struct pthread, member[0])),            \
319                        "r" (idx));                                            \
320      else                                                                     \
321        {                                                                      \
322          if (sizeof (__value) != 8)                                           \
323            /* There should not be any value with a size other than 1,         \
324               4 or 8.  */                                                     \
325            abort ();                                                          \
326                                                                               \
327          __asm__ __volatile__  ("movl %%gs:%P1(,%2,8),%%eax\n\t"                      \
328                         "movl %%gs:4+%P1(,%2,8),%%edx"                        \
329                         : "=&A" (__value)                                     \
330                         : "i" (offsetof (struct pthread, member[0])),         \
331                           "r" (idx));                                         \
332        }                                                                      \
333      __value; })
334
335
336 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
337 # define THREAD_SETMEM(descr, member, value) \
338   ({ if (sizeof (descr->member) == 1)                                         \
339        __asm__ __volatile__ ("movb %b0,%%gs:%P1" :                                    \
340                      : "iq" (value),                                          \
341                        "i" (offsetof (struct pthread, member)));              \
342      else if (sizeof (descr->member) == 4)                                    \
343        __asm__ __volatile__ ("movl %0,%%gs:%P1" :                                     \
344                      : "ir" (value),                                          \
345                        "i" (offsetof (struct pthread, member)));              \
346      else                                                                     \
347        {                                                                      \
348          if (sizeof (descr->member) != 8)                                     \
349            /* There should not be any value with a size other than 1,         \
350               4 or 8.  */                                                     \
351            abort ();                                                          \
352                                                                               \
353          __asm__ __volatile__ ("movl %%eax,%%gs:%P1\n\t"                              \
354                        "movl %%edx,%%gs:%P2" :                                \
355                        : "A" (value),                                         \
356                          "i" (offsetof (struct pthread, member)),             \
357                          "i" (offsetof (struct pthread, member) + 4));        \
358        }})
359
360
361 /* Set member of the thread descriptor directly.  */
362 # define THREAD_SETMEM_NC(descr, member, idx, value) \
363   ({ if (sizeof (descr->member[0]) == 1)                                      \
364        __asm__ __volatile__ ("movb %b0,%%gs:%P1(%2)" :                                \
365                      : "iq" (value),                                          \
366                        "i" (offsetof (struct pthread, member)),               \
367                        "r" (idx));                                            \
368      else if (sizeof (descr->member[0]) == 4)                                 \
369        __asm__ __volatile__ ("movl %0,%%gs:%P1(,%2,4)" :                              \
370                      : "ir" (value),                                          \
371                        "i" (offsetof (struct pthread, member)),               \
372                        "r" (idx));                                            \
373      else                                                                     \
374        {                                                                      \
375          if (sizeof (descr->member[0]) != 8)                                  \
376            /* There should not be any value with a size other than 1,         \
377               4 or 8.  */                                                     \
378            abort ();                                                          \
379                                                                               \
380          __asm__ __volatile__ ("movl %%eax,%%gs:%P1(,%2,8)\n\t"                       \
381                        "movl %%edx,%%gs:4+%P1(,%2,8)" :                       \
382                        : "A" (value),                                         \
383                          "i" (offsetof (struct pthread, member)),             \
384                          "r" (idx));                                          \
385        }})
386
387
388 /* Atomic compare and exchange on TLS, returning old value.  */
389 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
390   ({ __typeof (descr->member) __ret;                                          \
391      __typeof (oldval) __old = (oldval);                                      \
392      if (sizeof (descr->member) == 4)                                         \
393        __asm__ __volatile__ (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3"                      \
394                      : "=a" (__ret)                                           \
395                      : "0" (__old), "r" (newval),                             \
396                        "i" (offsetof (struct pthread, member)));              \
397      else                                                                     \
398        /* Not necessary for other sizes in the moment.  */                    \
399        abort ();                                                              \
400      __ret; })
401
402
403 /* Atomic logical and.  */
404 #define THREAD_ATOMIC_AND(descr, member, val) \
405   (void) ({ if (sizeof ((descr)->member) == 4)                                \
406               __asm__ __volatile__ (LOCK_PREFIX "andl %1, %%gs:%P0"                   \
407                             :: "i" (offsetof (struct pthread, member)),       \
408                                "ir" (val));                                   \
409             else                                                              \
410               /* Not necessary for other sizes in the moment.  */             \
411               abort (); })
412
413
414 /* Atomic set bit.  */
415 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
416   (void) ({ if (sizeof ((descr)->member) == 4)                                \
417               __asm__ __volatile__ (LOCK_PREFIX "orl %1, %%gs:%P0"                    \
418                             :: "i" (offsetof (struct pthread, member)),       \
419                                "ir" (1 << (bit)));                            \
420             else                                                              \
421               /* Not necessary for other sizes in the moment.  */             \
422               abort (); })
423
424
425 /* Call the user-provided thread function.  */
426 #define CALL_THREAD_FCT(descr) \
427   ({ void *__res;                                                             \
428      int __ignore1, __ignore2;                                                \
429      __asm__ __volatile__ ("pushl %%eax\n\t"                                          \
430                    "pushl %%eax\n\t"                                          \
431                    "pushl %%eax\n\t"                                          \
432                    "pushl %%gs:%P4\n\t"                                       \
433                    "call *%%gs:%P3\n\t"                                       \
434                    "addl $16, %%esp"                                          \
435                    : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2)         \
436                    : "i" (offsetof (struct pthread, start_routine)),          \
437                      "i" (offsetof (struct pthread, arg)));                   \
438      __res; })
439
440
441 /* Set the stack guard field in TCB head.  */
442 #define THREAD_SET_STACK_GUARD(value) \
443   THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
444 #define THREAD_COPY_STACK_GUARD(descr) \
445   ((descr)->header.stack_guard                                                \
446    = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
447
448
449 /* Set the pointer guard field in the TCB head.  */
450 #define THREAD_SET_POINTER_GUARD(value) \
451   THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
452 #define THREAD_COPY_POINTER_GUARD(descr) \
453   ((descr)->header.pointer_guard                                              \
454    = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
455
456
457 /* Get and set the global scope generation counter in the TCB head.  */
458 #define THREAD_GSCOPE_FLAG_UNUSED 0
459 #define THREAD_GSCOPE_FLAG_USED   1
460 #define THREAD_GSCOPE_FLAG_WAIT   2
461 #define THREAD_GSCOPE_RESET_FLAG() \
462   do                                                                          \
463     { int __res;                                                              \
464       __asm__ __volatile__ ("xchgl %0, %%gs:%P1"                                      \
465                     : "=r" (__res)                                            \
466                     : "i" (offsetof (struct pthread, header.gscope_flag)),    \
467                       "0" (THREAD_GSCOPE_FLAG_UNUSED));                       \
468       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                   \
469         lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);    \
470     }                                                                         \
471   while (0)
472 #define THREAD_GSCOPE_SET_FLAG() \
473   THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
474 #define THREAD_GSCOPE_WAIT() \
475   GL(dl_wait_lookup_done) ()
476
477 #endif /* __ASSEMBLER__ */
478
479 #endif  /* tls.h */