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