(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nptl / sysdeps / i386 / tls.h
blob945a4c71d65b273ff7373581f18714cd0d5c6777
1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
20 #ifndef _TLS_H
21 #define _TLS_H 1
23 #include <dl-sysdep.h>
24 #ifndef __ASSEMBLER__
25 # include <stddef.h>
26 # include <stdint.h>
27 # include <stdlib.h>
28 # include <list.h>
31 /* Type for the dtv. */
32 typedef union dtv
34 size_t counter;
35 void *pointer;
36 } dtv_t;
39 typedef struct
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;
49 # define TLS_MULTIPLE_THREADS_IN_TCB 1
51 #else /* __ASSEMBLER__ */
52 # include <tcb-offsets.h>
53 #endif
56 /* We require TLS support in the tools. */
57 #ifndef HAVE_TLS_SUPPORT
58 # error "TLS support is required."
59 #endif
61 /* Signal that TLS support is available. */
62 #define USE_TLS 1
64 /* Alignment requirement for the stack. For IA-32 this is governed by
65 the SSE memory functions. */
66 #define STACK_ALIGN 16
68 #ifndef __ASSEMBLER__
69 /* Get system call information. */
70 # include <sysdep.h>
72 /* The old way: using LDT. */
74 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
75 struct user_desc
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;
89 /* Initializing bit fields is slow. We speed it up by using a union. */
90 union user_desc_init
92 struct user_desc desc;
93 unsigned int vals[4];
97 /* Get the thread descriptor definition. */
98 # include <nptl/descr.h>
100 /* This is the size of the initial TCB. */
101 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
103 /* Alignment requirements for the initial TCB. */
104 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
106 /* This is the size of the TCB. */
107 # define TLS_TCB_SIZE sizeof (struct pthread)
109 /* Alignment requirements for the TCB. */
110 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
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
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
122 /* Install new dtv for current thread. */
123 # define INSTALL_NEW_DTV(dtvp) \
124 ({ struct pthread *__pd; \
125 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
127 /* Return dtv of given thread descriptor. */
128 # define GET_DTV(descr) \
129 (((tcbhead_t *) (descr))->dtv)
131 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
132 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
134 /* Macros to load from and store into segment registers. */
135 # ifndef TLS_GET_GS
136 # define TLS_GET_GS() \
137 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
138 # endif
139 # ifndef TLS_SET_GS
140 # define TLS_SET_GS(val) \
141 __asm ("movw %w0, %%gs" :: "q" (val))
142 # endif
145 # ifndef __NR_set_thread_area
146 # define __NR_set_thread_area 243
147 # endif
148 # ifndef TLS_FLAG_WRITABLE
149 # define TLS_FLAG_WRITABLE 0x00000001
150 # endif
152 // XXX Enable for the real world.
153 #if 0
154 # ifndef __ASSUME_SET_THREAD_AREA
155 # error "we need set_thread_area"
156 # endif
157 #endif
159 # ifdef __PIC__
160 # define TLS_EBX_ARG "r"
161 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
162 # else
163 # define TLS_EBX_ARG "b"
164 # define TLS_LOAD_EBX
165 # endif
167 #if defined NEED_DL_SYSINFO
168 # define INIT_SYSINFO \
169 _head->sysinfo = GLRO(dl_sysinfo)
170 #else
171 # define INIT_SYSINFO
172 #endif
174 #ifndef LOCK_PREFIX
175 # ifdef UP
176 # define LOCK_PREFIX /* nothing */
177 # else
178 # define LOCK_PREFIX "lock;"
179 # endif
180 #endif
182 /* Code to initially initialize the thread pointer. This might need
183 special attention since 'errno' is not yet available and if the
184 operation can cause a failure 'errno' must not be touched. */
185 # define TLS_INIT_TP(thrdescr, secondcall) \
186 ({ void *_thrdescr = (thrdescr); \
187 tcbhead_t *_head = _thrdescr; \
188 union user_desc_init _segdescr; \
189 int _result; \
191 _head->tcb = _thrdescr; \
192 /* For now the thread descriptor is at the same address. */ \
193 _head->self = _thrdescr; \
194 /* New syscall handling support. */ \
195 INIT_SYSINFO; \
197 /* The 'entry_number' field. Let the kernel pick a value. */ \
198 if (secondcall) \
199 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
200 else \
201 _segdescr.vals[0] = -1; \
202 /* The 'base_addr' field. Pointer to the TCB. */ \
203 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
204 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
205 _segdescr.vals[2] = 0xfffff; \
206 /* Collapsed value of the bitfield: \
207 .seg_32bit = 1 \
208 .contents = 0 \
209 .read_exec_only = 0 \
210 .limit_in_pages = 1 \
211 .seg_not_present = 0 \
212 .useable = 1 */ \
213 _segdescr.vals[3] = 0x51; \
215 /* Install the TLS. */ \
216 asm volatile (TLS_LOAD_EBX \
217 "int $0x80\n\t" \
218 TLS_LOAD_EBX \
219 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
220 : "0" (__NR_set_thread_area), \
221 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
223 if (_result == 0) \
224 /* We know the index in the GDT, now load the segment register. \
225 The use of the GDT is described by the value 3 in the lower \
226 three bits of the segment descriptor value. \
228 Note that we have to do this even if the numeric value of \
229 the descriptor does not change. Loading the segment register \
230 causes the segment information from the GDT to be loaded \
231 which is necessary since we have changed it. */ \
232 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
234 _result == 0 ? NULL \
235 : "set_thread_area failed when setting up thread-local storage\n"; })
238 /* Return the address of the dtv for the current thread. */
239 # define THREAD_DTV() \
240 ({ struct pthread *__pd; \
241 THREAD_GETMEM (__pd, header.dtv); })
244 /* Return the thread descriptor for the current thread.
246 The contained asm must *not* be marked volatile since otherwise
247 assignments like
248 pthread_descr self = thread_self();
249 do not get optimized away. */
250 # define THREAD_SELF \
251 ({ struct pthread *__self; \
252 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
253 : "i" (offsetof (struct pthread, header.self))); \
254 __self;})
256 /* Magic for libthread_db to know how to do THREAD_SELF. */
257 # define DB_THREAD_SELF \
258 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
259 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
262 /* Read member of the thread descriptor directly. */
263 # define THREAD_GETMEM(descr, member) \
264 ({ __typeof (descr->member) __value; \
265 if (sizeof (__value) == 1) \
266 asm volatile ("movb %%gs:%P2,%b0" \
267 : "=q" (__value) \
268 : "0" (0), "i" (offsetof (struct pthread, member))); \
269 else if (sizeof (__value) == 4) \
270 asm volatile ("movl %%gs:%P1,%0" \
271 : "=r" (__value) \
272 : "i" (offsetof (struct pthread, member))); \
273 else \
275 if (sizeof (__value) != 8) \
276 /* There should not be any value with a size other than 1, \
277 4 or 8. */ \
278 abort (); \
280 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
281 "movl %%gs:%P2,%%edx" \
282 : "=A" (__value) \
283 : "i" (offsetof (struct pthread, member)), \
284 "i" (offsetof (struct pthread, member) + 4)); \
286 __value; })
289 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
290 # define THREAD_GETMEM_NC(descr, member, idx) \
291 ({ __typeof (descr->member[0]) __value; \
292 if (sizeof (__value) == 1) \
293 asm volatile ("movb %%gs:%P2(%3),%b0" \
294 : "=q" (__value) \
295 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
296 "r" (idx)); \
297 else if (sizeof (__value) == 4) \
298 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
299 : "=r" (__value) \
300 : "i" (offsetof (struct pthread, member[0])), \
301 "r" (idx)); \
302 else \
304 if (sizeof (__value) != 8) \
305 /* There should not be any value with a size other than 1, \
306 4 or 8. */ \
307 abort (); \
309 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
310 "movl %%gs:4+%P1(,%2,8),%%edx" \
311 : "=&A" (__value) \
312 : "i" (offsetof (struct pthread, member[0])), \
313 "r" (idx)); \
315 __value; })
318 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
319 # define THREAD_SETMEM(descr, member, value) \
320 ({ if (sizeof (descr->member) == 1) \
321 asm volatile ("movb %b0,%%gs:%P1" : \
322 : "iq" (value), \
323 "i" (offsetof (struct pthread, member))); \
324 else if (sizeof (descr->member) == 4) \
325 asm volatile ("movl %0,%%gs:%P1" : \
326 : "ir" (value), \
327 "i" (offsetof (struct pthread, member))); \
328 else \
330 if (sizeof (descr->member) != 8) \
331 /* There should not be any value with a size other than 1, \
332 4 or 8. */ \
333 abort (); \
335 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
336 "movl %%edx,%%gs:%P2" : \
337 : "A" (value), \
338 "i" (offsetof (struct pthread, member)), \
339 "i" (offsetof (struct pthread, member) + 4)); \
343 /* Set member of the thread descriptor directly. */
344 # define THREAD_SETMEM_NC(descr, member, idx, value) \
345 ({ if (sizeof (descr->member[0]) == 1) \
346 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
347 : "iq" (value), \
348 "i" (offsetof (struct pthread, member)), \
349 "r" (idx)); \
350 else if (sizeof (descr->member[0]) == 4) \
351 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
352 : "ir" (value), \
353 "i" (offsetof (struct pthread, member)), \
354 "r" (idx)); \
355 else \
357 if (sizeof (descr->member[0]) != 8) \
358 /* There should not be any value with a size other than 1, \
359 4 or 8. */ \
360 abort (); \
362 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
363 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
364 : "A" (value), \
365 "i" (offsetof (struct pthread, member)), \
366 "r" (idx)); \
370 /* Atomic compare and exchange on TLS, returning old value. */
371 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
372 ({ __typeof (descr->member) __ret; \
373 __typeof (oldval) __old = (oldval); \
374 if (sizeof (descr->member) == 4) \
375 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
376 : "=a" (__ret) \
377 : "0" (__old), "r" (newval), \
378 "i" (offsetof (struct pthread, member))); \
379 else \
380 /* Not necessary for other sizes in the moment. */ \
381 abort (); \
382 __ret; })
385 /* Atomic set bit. */
386 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
387 (void) ({ if (sizeof ((descr)->member) == 4) \
388 asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
389 :: "i" (offsetof (struct pthread, member)), \
390 "ir" (1 << (bit))); \
391 else \
392 /* Not necessary for other sizes in the moment. */ \
393 abort (); })
396 /* Call the user-provided thread function. */
397 #define CALL_THREAD_FCT(descr) \
398 ({ void *__res; \
399 int __ignore1, __ignore2; \
400 asm volatile ("pushl %%eax\n\t" \
401 "pushl %%eax\n\t" \
402 "pushl %%eax\n\t" \
403 "pushl %%gs:%P4\n\t" \
404 "call *%%gs:%P3\n\t" \
405 "addl $16, %%esp" \
406 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
407 : "i" (offsetof (struct pthread, start_routine)), \
408 "i" (offsetof (struct pthread, arg))); \
409 __res; })
412 #endif /* __ASSEMBLER__ */
414 #endif /* tls.h */