OSDN Git Service

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