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