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