* sysdeps/i386/tls.h (TLS_INIT_TCB_SIZE, TLS_INIT_TCB_ALIGN): Define to
[glibc.git] / nptl / sysdeps / i386 / tls.h
blobc0331140f4059c863c65c8164d6cd1dcdcc981a1
1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002, 2003, 2004, 2005 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 <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
28 # include <stdlib.h>
29 # include <list.h>
32 /* Type for the dtv. */
33 typedef union dtv
35 size_t counter;
36 struct
38 void *val;
39 bool is_static;
40 } pointer;
41 } dtv_t;
44 typedef struct
46 void *tcb; /* Pointer to the TCB. Not necessarily the
47 thread descriptor used by libpthread. */
48 dtv_t *dtv;
49 void *self; /* Pointer to the thread descriptor. */
50 int multiple_threads;
51 uintptr_t sysinfo;
52 } tcbhead_t;
54 # define TLS_MULTIPLE_THREADS_IN_TCB 1
56 #else /* __ASSEMBLER__ */
57 # include <tcb-offsets.h>
58 #endif
61 /* We require TLS support in the tools. */
62 #ifndef HAVE_TLS_SUPPORT
63 # error "TLS support is required."
64 #endif
66 /* Signal that TLS support is available. */
67 #define USE_TLS 1
69 /* Alignment requirement for the stack. For IA-32 this is governed by
70 the SSE memory functions. */
71 #define STACK_ALIGN 16
73 #ifndef __ASSEMBLER__
74 /* Get system call information. */
75 # include <sysdep.h>
77 /* The old way: using LDT. */
79 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
80 struct user_desc
82 unsigned int entry_number;
83 unsigned long int base_addr;
84 unsigned int limit;
85 unsigned int seg_32bit:1;
86 unsigned int contents:2;
87 unsigned int read_exec_only:1;
88 unsigned int limit_in_pages:1;
89 unsigned int seg_not_present:1;
90 unsigned int useable:1;
91 unsigned int empty:25;
94 /* Initializing bit fields is slow. We speed it up by using a union. */
95 union user_desc_init
97 struct user_desc desc;
98 unsigned int vals[4];
102 /* Get the thread descriptor definition. */
103 # include <nptl/descr.h>
105 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
106 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
107 struct pthread even when not linked with -lpthread. */
108 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
110 /* Alignment requirements for the initial TCB. */
111 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
113 /* This is the size of the TCB. */
114 # define TLS_TCB_SIZE sizeof (struct pthread)
116 /* Alignment requirements for the TCB. */
117 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
119 /* The TCB can have any size and the memory following the address the
120 thread pointer points to is unspecified. Allocate the TCB there. */
121 # define TLS_TCB_AT_TP 1
124 /* Install the dtv pointer. The pointer passed is to the element with
125 index -1 which contain the length. */
126 # define INSTALL_DTV(descr, dtvp) \
127 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
129 /* Install new dtv for current thread. */
130 # define INSTALL_NEW_DTV(dtvp) \
131 ({ struct pthread *__pd; \
132 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
134 /* Return dtv of given thread descriptor. */
135 # define GET_DTV(descr) \
136 (((tcbhead_t *) (descr))->dtv)
138 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
139 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
141 /* Macros to load from and store into segment registers. */
142 # ifndef TLS_GET_GS
143 # define TLS_GET_GS() \
144 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
145 # endif
146 # ifndef TLS_SET_GS
147 # define TLS_SET_GS(val) \
148 __asm ("movw %w0, %%gs" :: "q" (val))
149 # endif
152 # ifndef __NR_set_thread_area
153 # define __NR_set_thread_area 243
154 # endif
155 # ifndef TLS_FLAG_WRITABLE
156 # define TLS_FLAG_WRITABLE 0x00000001
157 # endif
159 // XXX Enable for the real world.
160 #if 0
161 # ifndef __ASSUME_SET_THREAD_AREA
162 # error "we need set_thread_area"
163 # endif
164 #endif
166 # ifdef __PIC__
167 # define TLS_EBX_ARG "r"
168 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
169 # else
170 # define TLS_EBX_ARG "b"
171 # define TLS_LOAD_EBX
172 # endif
174 #if defined NEED_DL_SYSINFO
175 # define INIT_SYSINFO \
176 _head->sysinfo = GLRO(dl_sysinfo)
177 #else
178 # define INIT_SYSINFO
179 #endif
181 #ifndef LOCK_PREFIX
182 # ifdef UP
183 # define LOCK_PREFIX /* nothing */
184 # else
185 # define LOCK_PREFIX "lock;"
186 # endif
187 #endif
189 /* Code to initially initialize the thread pointer. This might need
190 special attention since 'errno' is not yet available and if the
191 operation can cause a failure 'errno' must not be touched. */
192 # define TLS_INIT_TP(thrdescr, secondcall) \
193 ({ void *_thrdescr = (thrdescr); \
194 tcbhead_t *_head = _thrdescr; \
195 union user_desc_init _segdescr; \
196 int _result; \
198 _head->tcb = _thrdescr; \
199 /* For now the thread descriptor is at the same address. */ \
200 _head->self = _thrdescr; \
201 /* New syscall handling support. */ \
202 INIT_SYSINFO; \
204 /* The 'entry_number' field. Let the kernel pick a value. */ \
205 if (secondcall) \
206 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
207 else \
208 _segdescr.vals[0] = -1; \
209 /* The 'base_addr' field. Pointer to the TCB. */ \
210 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
211 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
212 _segdescr.vals[2] = 0xfffff; \
213 /* Collapsed value of the bitfield: \
214 .seg_32bit = 1 \
215 .contents = 0 \
216 .read_exec_only = 0 \
217 .limit_in_pages = 1 \
218 .seg_not_present = 0 \
219 .useable = 1 */ \
220 _segdescr.vals[3] = 0x51; \
222 /* Install the TLS. */ \
223 asm volatile (TLS_LOAD_EBX \
224 "int $0x80\n\t" \
225 TLS_LOAD_EBX \
226 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
227 : "0" (__NR_set_thread_area), \
228 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
230 if (_result == 0) \
231 /* We know the index in the GDT, now load the segment register. \
232 The use of the GDT is described by the value 3 in the lower \
233 three bits of the segment descriptor value. \
235 Note that we have to do this even if the numeric value of \
236 the descriptor does not change. Loading the segment register \
237 causes the segment information from the GDT to be loaded \
238 which is necessary since we have changed it. */ \
239 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
241 _result == 0 ? NULL \
242 : "set_thread_area failed when setting up thread-local storage\n"; })
245 /* Return the address of the dtv for the current thread. */
246 # define THREAD_DTV() \
247 ({ struct pthread *__pd; \
248 THREAD_GETMEM (__pd, header.dtv); })
251 /* Return the thread descriptor for the current thread.
253 The contained asm must *not* be marked volatile since otherwise
254 assignments like
255 pthread_descr self = thread_self();
256 do not get optimized away. */
257 # define THREAD_SELF \
258 ({ struct pthread *__self; \
259 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
260 : "i" (offsetof (struct pthread, header.self))); \
261 __self;})
263 /* Magic for libthread_db to know how to do THREAD_SELF. */
264 # define DB_THREAD_SELF \
265 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
266 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
269 /* Read member of the thread descriptor directly. */
270 # define THREAD_GETMEM(descr, member) \
271 ({ __typeof (descr->member) __value; \
272 if (sizeof (__value) == 1) \
273 asm volatile ("movb %%gs:%P2,%b0" \
274 : "=q" (__value) \
275 : "0" (0), "i" (offsetof (struct pthread, member))); \
276 else if (sizeof (__value) == 4) \
277 asm volatile ("movl %%gs:%P1,%0" \
278 : "=r" (__value) \
279 : "i" (offsetof (struct pthread, member))); \
280 else \
282 if (sizeof (__value) != 8) \
283 /* There should not be any value with a size other than 1, \
284 4 or 8. */ \
285 abort (); \
287 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
288 "movl %%gs:%P2,%%edx" \
289 : "=A" (__value) \
290 : "i" (offsetof (struct pthread, member)), \
291 "i" (offsetof (struct pthread, member) + 4)); \
293 __value; })
296 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
297 # define THREAD_GETMEM_NC(descr, member, idx) \
298 ({ __typeof (descr->member[0]) __value; \
299 if (sizeof (__value) == 1) \
300 asm volatile ("movb %%gs:%P2(%3),%b0" \
301 : "=q" (__value) \
302 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
303 "r" (idx)); \
304 else if (sizeof (__value) == 4) \
305 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
306 : "=r" (__value) \
307 : "i" (offsetof (struct pthread, member[0])), \
308 "r" (idx)); \
309 else \
311 if (sizeof (__value) != 8) \
312 /* There should not be any value with a size other than 1, \
313 4 or 8. */ \
314 abort (); \
316 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
317 "movl %%gs:4+%P1(,%2,8),%%edx" \
318 : "=&A" (__value) \
319 : "i" (offsetof (struct pthread, member[0])), \
320 "r" (idx)); \
322 __value; })
325 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
326 # define THREAD_SETMEM(descr, member, value) \
327 ({ if (sizeof (descr->member) == 1) \
328 asm volatile ("movb %b0,%%gs:%P1" : \
329 : "iq" (value), \
330 "i" (offsetof (struct pthread, member))); \
331 else if (sizeof (descr->member) == 4) \
332 asm volatile ("movl %0,%%gs:%P1" : \
333 : "ir" (value), \
334 "i" (offsetof (struct pthread, member))); \
335 else \
337 if (sizeof (descr->member) != 8) \
338 /* There should not be any value with a size other than 1, \
339 4 or 8. */ \
340 abort (); \
342 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
343 "movl %%edx,%%gs:%P2" : \
344 : "A" (value), \
345 "i" (offsetof (struct pthread, member)), \
346 "i" (offsetof (struct pthread, member) + 4)); \
350 /* Set member of the thread descriptor directly. */
351 # define THREAD_SETMEM_NC(descr, member, idx, value) \
352 ({ if (sizeof (descr->member[0]) == 1) \
353 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
354 : "iq" (value), \
355 "i" (offsetof (struct pthread, member)), \
356 "r" (idx)); \
357 else if (sizeof (descr->member[0]) == 4) \
358 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
359 : "ir" (value), \
360 "i" (offsetof (struct pthread, member)), \
361 "r" (idx)); \
362 else \
364 if (sizeof (descr->member[0]) != 8) \
365 /* There should not be any value with a size other than 1, \
366 4 or 8. */ \
367 abort (); \
369 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
370 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
371 : "A" (value), \
372 "i" (offsetof (struct pthread, member)), \
373 "r" (idx)); \
377 /* Atomic compare and exchange on TLS, returning old value. */
378 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
379 ({ __typeof (descr->member) __ret; \
380 __typeof (oldval) __old = (oldval); \
381 if (sizeof (descr->member) == 4) \
382 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
383 : "=a" (__ret) \
384 : "0" (__old), "r" (newval), \
385 "i" (offsetof (struct pthread, member))); \
386 else \
387 /* Not necessary for other sizes in the moment. */ \
388 abort (); \
389 __ret; })
392 /* Atomic set bit. */
393 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
394 (void) ({ if (sizeof ((descr)->member) == 4) \
395 asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
396 :: "i" (offsetof (struct pthread, member)), \
397 "ir" (1 << (bit))); \
398 else \
399 /* Not necessary for other sizes in the moment. */ \
400 abort (); })
403 /* Call the user-provided thread function. */
404 #define CALL_THREAD_FCT(descr) \
405 ({ void *__res; \
406 int __ignore1, __ignore2; \
407 asm volatile ("pushl %%eax\n\t" \
408 "pushl %%eax\n\t" \
409 "pushl %%eax\n\t" \
410 "pushl %%gs:%P4\n\t" \
411 "call *%%gs:%P3\n\t" \
412 "addl $16, %%esp" \
413 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
414 : "i" (offsetof (struct pthread, start_routine)), \
415 "i" (offsetof (struct pthread, arg))); \
416 __res; })
419 #endif /* __ASSEMBLER__ */
421 #endif /* tls.h */