OSDN Git Service

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