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