Cast to uint64_t for 64-bit store
[glibc.git] / nptl / sysdeps / i386 / tls.h
blob65497cf922802ae132e6f06d14de38a0a64f17de
1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002-2007, 2009, 2011 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, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _TLS_H
20 #define _TLS_H 1
22 #include <dl-sysdep.h>
23 #ifndef __ASSEMBLER__
24 # include <stdbool.h>
25 # include <stddef.h>
26 # include <stdint.h>
27 # include <stdlib.h>
28 # include <sysdep.h>
29 # include <kernel-features.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 uintptr_t stack_guard;
53 uintptr_t pointer_guard;
54 int gscope_flag;
55 #ifndef __ASSUME_PRIVATE_FUTEX
56 int private_futex;
57 #else
58 int __unused1;
59 #endif
60 /* Reservation of some values for the TM ABI. */
61 void *__private_tm[5];
62 } tcbhead_t;
64 # define TLS_MULTIPLE_THREADS_IN_TCB 1
66 #else /* __ASSEMBLER__ */
67 # include <tcb-offsets.h>
68 #endif
71 /* Alignment requirement for the stack. For IA-32 this is governed by
72 the SSE memory functions. */
73 #define STACK_ALIGN 16
75 #ifndef __ASSEMBLER__
76 /* Get system call information. */
77 # include <sysdep.h>
79 /* The old way: using LDT. */
81 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
82 struct user_desc
84 unsigned int entry_number;
85 unsigned long int base_addr;
86 unsigned int limit;
87 unsigned int seg_32bit:1;
88 unsigned int contents:2;
89 unsigned int read_exec_only:1;
90 unsigned int limit_in_pages:1;
91 unsigned int seg_not_present:1;
92 unsigned int useable:1;
93 unsigned int empty:25;
96 /* Initializing bit fields is slow. We speed it up by using a union. */
97 union user_desc_init
99 struct user_desc desc;
100 unsigned int vals[4];
104 /* Get the thread descriptor definition. */
105 # include <nptl/descr.h>
107 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
108 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
109 struct pthread even when not linked with -lpthread. */
110 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
112 /* Alignment requirements for the initial TCB. */
113 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
115 /* This is the size of the TCB. */
116 # define TLS_TCB_SIZE sizeof (struct pthread)
118 /* Alignment requirements for the TCB. */
119 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
121 /* The TCB can have any size and the memory following the address the
122 thread pointer points to is unspecified. Allocate the TCB there. */
123 # define TLS_TCB_AT_TP 1
126 /* Install the dtv pointer. The pointer passed is to the element with
127 index -1 which contain the length. */
128 # define INSTALL_DTV(descr, dtvp) \
129 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
131 /* Install new dtv for current thread. */
132 # define INSTALL_NEW_DTV(dtvp) \
133 ({ struct pthread *__pd; \
134 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
136 /* Return dtv of given thread descriptor. */
137 # define GET_DTV(descr) \
138 (((tcbhead_t *) (descr))->dtv)
140 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
141 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
143 /* Macros to load from and store into segment registers. */
144 # ifndef TLS_GET_GS
145 # define TLS_GET_GS() \
146 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
147 # endif
148 # ifndef TLS_SET_GS
149 # define TLS_SET_GS(val) \
150 __asm ("movw %w0, %%gs" :: "q" (val))
151 # endif
154 # ifndef __NR_set_thread_area
155 # define __NR_set_thread_area 243
156 # endif
157 # ifndef TLS_FLAG_WRITABLE
158 # define TLS_FLAG_WRITABLE 0x00000001
159 # endif
161 // XXX Enable for the real world.
162 #if 0
163 # ifndef __ASSUME_SET_THREAD_AREA
164 # error "we need set_thread_area"
165 # endif
166 #endif
168 # ifdef __PIC__
169 # define TLS_EBX_ARG "r"
170 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
171 # else
172 # define TLS_EBX_ARG "b"
173 # define TLS_LOAD_EBX
174 # endif
176 #if defined NEED_DL_SYSINFO
177 # define INIT_SYSINFO \
178 _head->sysinfo = GLRO(dl_sysinfo)
179 #else
180 # define INIT_SYSINFO
181 #endif
183 #ifndef LOCK_PREFIX
184 # ifdef UP
185 # define LOCK_PREFIX /* nothing */
186 # else
187 # define LOCK_PREFIX "lock;"
188 # endif
189 #endif
191 /* Code to initially initialize the thread pointer. This might need
192 special attention since 'errno' is not yet available and if the
193 operation can cause a failure 'errno' must not be touched. */
194 # define TLS_INIT_TP(thrdescr, secondcall) \
195 ({ void *_thrdescr = (thrdescr); \
196 tcbhead_t *_head = _thrdescr; \
197 union user_desc_init _segdescr; \
198 int _result; \
200 _head->tcb = _thrdescr; \
201 /* For now the thread descriptor is at the same address. */ \
202 _head->self = _thrdescr; \
203 /* New syscall handling support. */ \
204 INIT_SYSINFO; \
206 /* The 'entry_number' field. Let the kernel pick a value. */ \
207 if (secondcall) \
208 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
209 else \
210 _segdescr.vals[0] = -1; \
211 /* The 'base_addr' field. Pointer to the TCB. */ \
212 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
213 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
214 _segdescr.vals[2] = 0xfffff; \
215 /* Collapsed value of the bitfield: \
216 .seg_32bit = 1 \
217 .contents = 0 \
218 .read_exec_only = 0 \
219 .limit_in_pages = 1 \
220 .seg_not_present = 0 \
221 .useable = 1 */ \
222 _segdescr.vals[3] = 0x51; \
224 /* Install the TLS. */ \
225 asm volatile (TLS_LOAD_EBX \
226 "int $0x80\n\t" \
227 TLS_LOAD_EBX \
228 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
229 : "0" (__NR_set_thread_area), \
230 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
232 if (_result == 0) \
233 /* We know the index in the GDT, now load the segment register. \
234 The use of the GDT is described by the value 3 in the lower \
235 three bits of the segment descriptor value. \
237 Note that we have to do this even if the numeric value of \
238 the descriptor does not change. Loading the segment register \
239 causes the segment information from the GDT to be loaded \
240 which is necessary since we have changed it. */ \
241 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
243 _result == 0 ? NULL \
244 : "set_thread_area failed when setting up thread-local storage\n"; })
247 /* Return the address of the dtv for the current thread. */
248 # define THREAD_DTV() \
249 ({ struct pthread *__pd; \
250 THREAD_GETMEM (__pd, header.dtv); })
253 /* Return the thread descriptor for the current thread.
255 The contained asm must *not* be marked volatile since otherwise
256 assignments like
257 pthread_descr self = thread_self();
258 do not get optimized away. */
259 # define THREAD_SELF \
260 ({ struct pthread *__self; \
261 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
262 : "i" (offsetof (struct pthread, header.self))); \
263 __self;})
265 /* Magic for libthread_db to know how to do THREAD_SELF. */
266 # define DB_THREAD_SELF \
267 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
268 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
271 /* Read member of the thread descriptor directly. */
272 # define THREAD_GETMEM(descr, member) \
273 ({ __typeof (descr->member) __value; \
274 if (sizeof (__value) == 1) \
275 asm volatile ("movb %%gs:%P2,%b0" \
276 : "=q" (__value) \
277 : "0" (0), "i" (offsetof (struct pthread, member))); \
278 else if (sizeof (__value) == 4) \
279 asm volatile ("movl %%gs:%P1,%0" \
280 : "=r" (__value) \
281 : "i" (offsetof (struct pthread, member))); \
282 else \
284 if (sizeof (__value) != 8) \
285 /* There should not be any value with a size other than 1, \
286 4 or 8. */ \
287 abort (); \
289 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
290 "movl %%gs:%P2,%%edx" \
291 : "=A" (__value) \
292 : "i" (offsetof (struct pthread, member)), \
293 "i" (offsetof (struct pthread, member) + 4)); \
295 __value; })
298 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
299 # define THREAD_GETMEM_NC(descr, member, idx) \
300 ({ __typeof (descr->member[0]) __value; \
301 if (sizeof (__value) == 1) \
302 asm volatile ("movb %%gs:%P2(%3),%b0" \
303 : "=q" (__value) \
304 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
305 "r" (idx)); \
306 else if (sizeof (__value) == 4) \
307 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
308 : "=r" (__value) \
309 : "i" (offsetof (struct pthread, member[0])), \
310 "r" (idx)); \
311 else \
313 if (sizeof (__value) != 8) \
314 /* There should not be any value with a size other than 1, \
315 4 or 8. */ \
316 abort (); \
318 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
319 "movl %%gs:4+%P1(,%2,8),%%edx" \
320 : "=&A" (__value) \
321 : "i" (offsetof (struct pthread, member[0])), \
322 "r" (idx)); \
324 __value; })
327 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
328 # define THREAD_SETMEM(descr, member, value) \
329 ({ if (sizeof (descr->member) == 1) \
330 asm volatile ("movb %b0,%%gs:%P1" : \
331 : "iq" (value), \
332 "i" (offsetof (struct pthread, member))); \
333 else if (sizeof (descr->member) == 4) \
334 asm volatile ("movl %0,%%gs:%P1" : \
335 : "ir" (value), \
336 "i" (offsetof (struct pthread, member))); \
337 else \
339 if (sizeof (descr->member) != 8) \
340 /* There should not be any value with a size other than 1, \
341 4 or 8. */ \
342 abort (); \
344 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
345 "movl %%edx,%%gs:%P2" : \
346 : "A" ((uint64_t) (value)), \
347 "i" (offsetof (struct pthread, member)), \
348 "i" (offsetof (struct pthread, member) + 4)); \
352 /* Set member of the thread descriptor directly. */
353 # define THREAD_SETMEM_NC(descr, member, idx, value) \
354 ({ if (sizeof (descr->member[0]) == 1) \
355 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
356 : "iq" (value), \
357 "i" (offsetof (struct pthread, member)), \
358 "r" (idx)); \
359 else if (sizeof (descr->member[0]) == 4) \
360 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
361 : "ir" (value), \
362 "i" (offsetof (struct pthread, member)), \
363 "r" (idx)); \
364 else \
366 if (sizeof (descr->member[0]) != 8) \
367 /* There should not be any value with a size other than 1, \
368 4 or 8. */ \
369 abort (); \
371 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
372 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
373 : "A" ((uint64_t) (value)), \
374 "i" (offsetof (struct pthread, member)), \
375 "r" (idx)); \
379 /* Atomic compare and exchange on TLS, returning old value. */
380 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
381 ({ __typeof (descr->member) __ret; \
382 __typeof (oldval) __old = (oldval); \
383 if (sizeof (descr->member) == 4) \
384 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
385 : "=a" (__ret) \
386 : "0" (__old), "r" (newval), \
387 "i" (offsetof (struct pthread, member))); \
388 else \
389 /* Not necessary for other sizes in the moment. */ \
390 abort (); \
391 __ret; })
394 /* Atomic logical and. */
395 #define THREAD_ATOMIC_AND(descr, member, val) \
396 (void) ({ if (sizeof ((descr)->member) == 4) \
397 asm volatile (LOCK_PREFIX "andl %1, %%gs:%P0" \
398 :: "i" (offsetof (struct pthread, member)), \
399 "ir" (val)); \
400 else \
401 /* Not necessary for other sizes in the moment. */ \
402 abort (); })
405 /* Atomic set bit. */
406 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
407 (void) ({ if (sizeof ((descr)->member) == 4) \
408 asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
409 :: "i" (offsetof (struct pthread, member)), \
410 "ir" (1 << (bit))); \
411 else \
412 /* Not necessary for other sizes in the moment. */ \
413 abort (); })
416 /* Call the user-provided thread function. */
417 #define CALL_THREAD_FCT(descr) \
418 ({ void *__res; \
419 int __ignore1, __ignore2; \
420 asm volatile ("pushl %%eax\n\t" \
421 "pushl %%eax\n\t" \
422 "pushl %%eax\n\t" \
423 "pushl %%gs:%P4\n\t" \
424 "call *%%gs:%P3\n\t" \
425 "addl $16, %%esp" \
426 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
427 : "i" (offsetof (struct pthread, start_routine)), \
428 "i" (offsetof (struct pthread, arg))); \
429 __res; })
432 /* Set the stack guard field in TCB head. */
433 #define THREAD_SET_STACK_GUARD(value) \
434 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
435 #define THREAD_COPY_STACK_GUARD(descr) \
436 ((descr)->header.stack_guard \
437 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
440 /* Set the pointer guard field in the TCB head. */
441 #define THREAD_SET_POINTER_GUARD(value) \
442 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
443 #define THREAD_COPY_POINTER_GUARD(descr) \
444 ((descr)->header.pointer_guard \
445 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
448 /* Get and set the global scope generation counter in the TCB head. */
449 #define THREAD_GSCOPE_FLAG_UNUSED 0
450 #define THREAD_GSCOPE_FLAG_USED 1
451 #define THREAD_GSCOPE_FLAG_WAIT 2
452 #define THREAD_GSCOPE_RESET_FLAG() \
453 do \
454 { int __res; \
455 asm volatile ("xchgl %0, %%gs:%P1" \
456 : "=r" (__res) \
457 : "i" (offsetof (struct pthread, header.gscope_flag)), \
458 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
459 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
460 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
462 while (0)
463 #define THREAD_GSCOPE_SET_FLAG() \
464 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
465 #define THREAD_GSCOPE_WAIT() \
466 GL(dl_wait_lookup_done) ()
468 #endif /* __ASSEMBLER__ */
470 #endif /* tls.h */