(tcbhead_t): Add list and multiple_threads fields. Define
[kopensolaris-gnu/glibc.git] / nptl / sysdeps / i386 / tls.h
1 /* Definition for thread-local data handling.  nptl/i386 version.
2    Copyright (C) 2002 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 <stddef.h>
25 # include <stdint.h>
26 # include <stdlib.h>
27 # include <list.h>
28
29
30 /* Type for the dtv.  */
31 typedef union dtv
32 {
33   size_t counter;
34   void *pointer;
35 } dtv_t;
36
37
38 typedef struct
39 {
40   void *tcb;            /* Pointer to the TCB.  Not necessary the
41                            thread descriptor used by libpthread.  */
42   dtv_t *dtv;
43   void *self;           /* Pointer to the thread descriptor.  */
44   list_t list;
45   int multiple_threads;
46 } tcbhead_t;
47 #endif
48
49
50 /* We require TLS support in the tools.  */
51 #ifndef HAVE_TLS_SUPPORT
52 # error "TLS support is required."
53 #endif
54
55 /* Signal that TLS support is available.  */
56 #define USE_TLS 1
57
58 /* Alignment requirement for the stack.  For IA-32 this is governed by
59    the SSE memory functions.  */
60 #define STACK_ALIGN     16
61
62 /* Offset of the MULTIPLE_THREADS element in tcbhead_t.  */
63 #define MULTIPLE_THREADS_OFFSET 20
64
65
66 #ifndef __ASSEMBLER__
67 /* Get system call information.  */
68 # include <sysdep.h>
69
70 /* The old way: using LDT.  */
71
72 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls.  */
73 struct user_desc
74 {
75   unsigned int entry_number;
76   unsigned long int base_addr;
77   unsigned int limit;
78   unsigned int seg_32bit:1;
79   unsigned int contents:2;
80   unsigned int read_exec_only:1;
81   unsigned int limit_in_pages:1;
82   unsigned int seg_not_present:1;
83   unsigned int useable:1;
84   unsigned int empty:25;
85 };
86
87 /* Initializing bit fields is slow.  We speed it up by using a union.  */
88 union user_desc_init
89 {
90   struct user_desc desc;
91   unsigned int vals[4];
92 };
93
94
95 /* Get the thread descriptor definition.  */
96 # include <nptl/descr.h>
97
98 /* This is the size of the initial TCB.  */
99 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
100
101 /* Alignment requirements for the initial TCB.  */
102 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
103
104 /* This is the size of the TCB.  */
105 # define TLS_TCB_SIZE sizeof (struct pthread)
106
107 /* Alignment requirements for the TCB.  */
108 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
109
110 /* The TCB can have any size and the memory following the address the
111    thread pointer points to is unspecified.  Allocate the TCB there.  */
112 # define TLS_TCB_AT_TP  1
113
114
115 /* Install the dtv pointer.  The pointer passed is to the element with
116    index -1 which contain the length.  */
117 # define INSTALL_DTV(descr, dtvp) \
118   ((tcbhead_t *) (descr))->dtv = dtvp + 1
119
120 /* Install new dtv for current thread.  */
121 # define INSTALL_NEW_DTV(dtv) \
122   ({ struct pthread *__pd;                                                    \
123      THREAD_SETMEM (__pd, header.data.dtvp, dtv); })
124
125 /* Return dtv of given thread descriptor.  */
126 # define GET_DTV(descr) \
127   (((tcbhead_t *) (descr))->dtv)
128
129
130 /* Macros to load from and store into segment registers.  */
131 # ifndef TLS_GET_GS
132 #  define TLS_GET_GS() \
133   ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
134 # endif
135 # ifndef TLS_SET_GS
136 #  define TLS_SET_GS(val) \
137   __asm ("movw %w0, %%gs" :: "q" (val))
138 # endif
139
140
141 # ifndef __NR_set_thread_area
142 #  define __NR_set_thread_area 243
143 # endif
144 # ifndef TLS_FLAG_WRITABLE
145 #  define TLS_FLAG_WRITABLE             0x00000001
146 # endif
147
148 // XXX Enable for the real world.
149 #if 0
150 # ifndef __ASSUME_SET_THREAD_AREA
151 #  error "we need set_thread_area"
152 # endif
153 #endif
154
155 # ifdef __PIC__
156 #  define TLS_EBX_ARG "r"
157 #  define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
158 # else
159 #  define TLS_EBX_ARG "b"
160 #  define TLS_LOAD_EBX
161 # endif
162
163 /* Code to initially initialize the thread pointer.  This might need
164    special attention since 'errno' is not yet available and if the
165    operation can cause a failure 'errno' must not be touched.  */
166 # define TLS_INIT_TP(thrdescr, secondcall) \
167   ({ void *_thrdescr = (thrdescr);                                            \
168      tcbhead_t *_head = _thrdescr;                                            \
169      union user_desc_init _segdescr;                                          \
170      int _result;                                                             \
171                                                                               \
172      _head->tcb = _thrdescr;                                                  \
173      /* For now the thread descriptor is at the same address.  */             \
174      _head->self = _thrdescr;                                                 \
175                                                                               \
176      /* The 'entry_number' field.  Let the kernel pick a value.  */           \
177      if (secondcall)                                                          \
178        _segdescr.vals[0] = TLS_GET_GS () >> 3;                                \
179      else                                                                     \
180        _segdescr.vals[0] = -1;                                                \
181      /* The 'base_addr' field.  Pointer to the TCB.  */                       \
182      _segdescr.vals[1] = (unsigned long int) _thrdescr;                       \
183      /* The 'limit' field.  We use 4GB which is 0xfffff pages.  */            \
184      _segdescr.vals[2] = 0xfffff;                                             \
185      /* Collapsed value of the bitfield:                                      \
186           .seg_32bit = 1                                                      \
187           .contents = 0                                                       \
188           .read_exec_only = 0                                                 \
189           .limit_in_pages = 1                                                 \
190           .seg_not_present = 0                                                \
191           .useable = 1 */                                                     \
192      _segdescr.vals[3] = 0x51;                                                \
193                                                                               \
194      /* Install the TLS.  */                                                  \
195      asm volatile (TLS_LOAD_EBX                                               \
196                    "int $0x80\n\t"                                            \
197                    TLS_LOAD_EBX                                               \
198                    : "=a" (_result), "=m" (_segdescr.desc.entry_number)       \
199                    : "0" (__NR_set_thread_area),                              \
200                      TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc));    \
201                                                                               \
202      if (_result == 0)                                                        \
203        /* We know the index in the GDT, now load the segment register.        \
204           The use of the GDT is described by the value 3 in the lower         \
205           three bits of the segment descriptor value.                         \
206                                                                               \
207           Note that we have to do this even if the numeric value of           \
208           the descriptor does not change.  Loading the segment register       \
209           causes the segment information from the GDT to be loaded            \
210           which is necessary since we have changed it.   */                   \
211        TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3);                      \
212                                                                               \
213      _result == 0 ? NULL                                                      \
214      : "set_thread_area failed when setting up thread-local storage"; })
215
216
217 /* Return the address of the dtv for the current thread.  */
218 # define THREAD_DTV() \
219   ({ struct pthread *__pd;                                                    \
220      THREAD_GETMEM (__pd, header.data.dtvp); })
221
222
223 /* Return the thread descriptor for the current thread.
224
225    The contained asm must *not* be marked volatile since otherwise
226    assignments like
227         pthread_descr self = thread_self();
228    do not get optimized away.  */
229 # define THREAD_SELF \
230   ({ struct pthread *__self;                                                  \
231      asm ("movl %%gs:%c1,%0" : "=r" (__self)                                  \
232           : "i" (offsetof (struct pthread, header.data.self)));               \
233      __self;})
234
235
236 /* Read member of the thread descriptor directly.  */
237 # define THREAD_GETMEM(descr, member) \
238   ({ __typeof (descr->member) __value;                                        \
239      if (sizeof (__value) == 1)                                               \
240        asm ("movb %%gs:%P2,%b0"                                               \
241             : "=q" (__value)                                                  \
242             : "0" (0), "i" (offsetof (struct pthread, member)));              \
243      else if (sizeof (__value) == 4)                                          \
244        asm ("movl %%gs:%P1,%0"                                                \
245             : "=r" (__value)                                                  \
246             : "i" (offsetof (struct pthread, member)));                       \
247      else                                                                     \
248        {                                                                      \
249          if (sizeof (__value) != 8)                                           \
250            /* There should not be any value with a size other than 1,         \
251               4 or 8.  */                                                     \
252            abort ();                                                          \
253                                                                               \
254          asm ("movl %%gs:%P1,%%eax\n\t"                                       \
255               "movl %%gs:%P2,%%edx"                                           \
256               : "=A" (__value)                                                \
257               : "i" (offsetof (struct pthread, member)),                      \
258                 "i" (offsetof (struct pthread, member) + 4));                 \
259        }                                                                      \
260      __value; })
261
262
263 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
264 # define THREAD_GETMEM_NC(descr, member, idx) \
265   ({ __typeof (descr->member[0]) __value;                                     \
266      if (sizeof (__value) == 1)                                               \
267        asm ("movb %%gs:%P2(%3),%b0"                                           \
268             : "=q" (__value)                                                  \
269             : "0" (0), "i" (offsetof (struct pthread, member[0])),            \
270               "r" (idx));                                                     \
271      else if (sizeof (__value) == 4)                                          \
272        asm ("movl %%gs:%P1(,%2,4),%0"                                         \
273             : "=r" (__value)                                                  \
274             : "i" (offsetof (struct pthread, member[0])), "r" (idx));         \
275      else                                                                     \
276        {                                                                      \
277          if (sizeof (__value) != 8)                                           \
278            /* There should not be any value with a size other than 1,         \
279               4 or 8.  */                                                     \
280            abort ();                                                          \
281                                                                               \
282          asm ("movl %%gs:%P1(,%2,8),%%eax\n\t"                                \
283               "movl %%gs:4+%P1(,%2,8),%%edx"                                  \
284               : "=&A" (__value)                                               \
285               : "i" (offsetof (struct pthread, member[0])), "r" (idx));       \
286        }                                                                      \
287      __value; })
288
289
290 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
291 # define THREAD_SETMEM(descr, member, value) \
292   ({ if (sizeof (descr->member) == 1)                                         \
293        asm volatile ("movb %0,%%gs:%P1" :                                     \
294                      : "iq" (value),                                          \
295                        "i" (offsetof (struct pthread, member)));              \
296      else if (sizeof (descr->member) == 4)                                    \
297        asm volatile ("movl %0,%%gs:%P1" :                                     \
298                      : "ir" (value),                                          \
299                        "i" (offsetof (struct pthread, member)));              \
300      else                                                                     \
301        {                                                                      \
302          if (sizeof (descr->member) != 8)                                     \
303            /* There should not be any value with a size other than 1,         \
304               4 or 8.  */                                                     \
305            abort ();                                                          \
306                                                                               \
307          asm volatile ("movl %%eax,%%gs:%P1\n\t"                              \
308                        "movl %%edx,%%gs:%P2" :                                \
309                        : "A" (value),                                         \
310                          "i" (offsetof (struct pthread, member)),             \
311                          "i" (offsetof (struct pthread, member) + 4));        \
312        }})
313
314
315 /* Set member of the thread descriptor directly.  */
316 # define THREAD_SETMEM_NC(descr, member, idx, value) \
317   ({ if (sizeof (descr->member[0]) == 1)                                      \
318        asm volatile ("movb %0,%%gs:%P1(%2)" :                                 \
319                      : "iq" (value),                                          \
320                        "i" (offsetof (struct pthread, member)),               \
321                        "r" (idx));                                            \
322      else if (sizeof (descr->member[0]) == 4)                                 \
323        asm volatile ("movl %0,%%gs:%P1(,%2,4)" :                              \
324                      : "ir" (value),                                          \
325                        "i" (offsetof (struct pthread, member)),               \
326                        "r" (idx));                                            \
327      else                                                                     \
328        {                                                                      \
329          if (sizeof (descr->member[0]) != 8)                                  \
330            /* There should not be any value with a size other than 1,         \
331               4 or 8.  */                                                     \
332            abort ();                                                          \
333                                                                               \
334          asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t"                       \
335                        "movl %%edx,%%gs:4+%P1(,%2,8)" :                       \
336                        : "A" (value),                                         \
337                          "i" (offsetof (struct pthread, member)),             \
338                          "r" (idx));                                          \
339        }})
340
341
342 #endif /* __ASSEMBLER__ */
343
344 #endif  /* tls.h */