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