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