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