(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / linuxthreads / sysdeps / i386 / tls.h
blob5306d082bb423bc2ed8a750a955d1b2be7a753b7
1 /* Definition for thread-local data handling. linuxthreads/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
23 # include <dl-sysdep.h>
24 # include <pt-machine.h>
26 #ifndef __ASSEMBLER__
27 # include <stddef.h>
28 # include <stdint.h>
30 /* Type for the dtv. */
31 typedef union dtv
33 size_t counter;
34 void *pointer;
35 } dtv_t;
38 typedef struct
40 void *tcb; /* Pointer to the TCB. Not necessary the
41 thread descriptor used by libpthread. */
42 dtv_t *dtv;
43 void *self; /* Pointer to the thread descriptor. */
44 int multiple_threads;
45 #ifdef NEED_DL_SYSINFO
46 uintptr_t sysinfo;
47 #endif
48 } tcbhead_t;
50 #else /* __ASSEMBLER__ */
51 # include <tcb-offsets.h>
52 #endif
54 /* We can support TLS only if the floating-stack support is available.
55 However, we want to compile in the support and test at runtime whether
56 the running kernel can support it or not. To avoid bothering with the
57 TLS support code at all, use configure --without-tls.
59 We need USE_TLS to be consistently defined, for ldsodefs.h conditionals.
60 But some of the code below can cause problems in building libpthread
61 (e.g. useldt.h will defined FLOATING_STACKS when it shouldn't). */
63 #if defined HAVE_TLS_SUPPORT \
64 && (defined FLOATING_STACKS || !defined IS_IN_libpthread)
66 /* Signal that TLS support is available. */
67 # define USE_TLS 1
69 # ifndef __ASSEMBLER__
70 /* Get system call information. */
71 # include <sysdep.h>
74 /* Get the thread descriptor definition. */
75 # include <linuxthreads/descr.h>
77 /* This is the size of the initial TCB. */
78 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
80 /* Alignment requirements for the initial TCB. */
81 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
83 /* This is the size of the TCB. */
84 # define TLS_TCB_SIZE sizeof (struct _pthread_descr_struct)
86 /* Alignment requirements for the TCB. */
87 # define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
89 /* The TCB can have any size and the memory following the address the
90 thread pointer points to is unspecified. Allocate the TCB there. */
91 # define TLS_TCB_AT_TP 1
94 /* Install the dtv pointer. The pointer passed is to the element with
95 index -1 which contain the length. */
96 # define INSTALL_DTV(descr, dtvp) \
97 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
99 /* Install new dtv for current thread. */
100 # define INSTALL_NEW_DTV(dtv) \
101 ({ struct _pthread_descr_struct *__descr; \
102 THREAD_SETMEM (__descr, p_header.data.dtvp, (dtv)); })
104 /* Return dtv of given thread descriptor. */
105 # define GET_DTV(descr) \
106 (((tcbhead_t *) (descr))->dtv)
108 # ifdef __PIC__
109 # define TLS_EBX_ARG "r"
110 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
111 # else
112 # define TLS_EBX_ARG "b"
113 # define TLS_LOAD_EBX
114 # endif
116 # if !defined IS_IN_linuxthreads && !defined DO_MODIFY_LDT
117 # include "useldt.h" /* For the structure. */
118 # endif
119 # if __ASSUME_LDT_WORKS > 0
120 # define TLS_DO_MODIFY_LDT_KERNEL_CHECK(doit) (doit) /* Nothing to check. */
121 # else
122 # define TLS_DO_MODIFY_LDT_KERNEL_CHECK(doit) \
123 (__builtin_expect (GLRO(dl_osversion) < 131939, 0) \
124 ? "kernel too old for thread-local storage support\n" \
125 : (doit))
126 # endif
128 # define TLS_DO_MODIFY_LDT(descr, nr) \
129 TLS_DO_MODIFY_LDT_KERNEL_CHECK( \
130 ({ \
131 struct modify_ldt_ldt_s ldt_entry = \
132 { nr, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
133 1, 0, 0, 1, 0, 1, 0 }; \
134 int result; \
135 asm volatile (TLS_LOAD_EBX \
136 "int $0x80\n\t" \
137 TLS_LOAD_EBX \
138 : "=a" (result) \
139 : "0" (__NR_modify_ldt), \
140 /* The extra argument with the "m" constraint is necessary \
141 to let the compiler know that we are accessing LDT_ENTRY \
142 here. */ \
143 "m" (ldt_entry), TLS_EBX_ARG (1), "c" (&ldt_entry), \
144 "d" (sizeof (ldt_entry))); \
145 __builtin_expect (result, 0) == 0 \
146 ? ({ asm ("movw %w0, %%gs" : : "q" ((nr) * 8 + 7)); NULL; }) \
147 : "cannot set up LDT for thread-local storage\n"; \
150 # define TLS_DO_SET_THREAD_AREA(descr, secondcall) \
151 ({ \
152 struct modify_ldt_ldt_s ldt_entry = \
153 { -1, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
154 1, 0, 0, 1, 0, 1, 0 }; \
155 int result; \
156 if (secondcall) \
157 ldt_entry.entry_number = ({ int _gs; \
158 asm ("movw %%gs, %w0" : "=q" (_gs)); \
159 (_gs & 0xffff) >> 3; }); \
160 asm volatile (TLS_LOAD_EBX \
161 "int $0x80\n\t" \
162 TLS_LOAD_EBX \
163 : "=a" (result), "=m" (ldt_entry.entry_number) \
164 : "0" (__NR_set_thread_area), \
165 /* The extra argument with the "m" constraint is necessary \
166 to let the compiler know that we are accessing LDT_ENTRY \
167 here. */ \
168 TLS_EBX_ARG (&ldt_entry), "m" (ldt_entry)); \
169 if (__builtin_expect (result, 0) == 0) \
170 asm ("movw %w0, %%gs" : : "q" (ldt_entry.entry_number * 8 + 3)); \
171 result; \
174 # ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
175 # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
176 (TLS_DO_SET_THREAD_AREA (descr, secondcall) \
177 ? "set_thread_area failed when setting up thread-local storage\n" : NULL)
178 # elif defined __NR_set_thread_area
179 # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
180 (TLS_DO_SET_THREAD_AREA (descr, secondcall) \
181 ? TLS_DO_MODIFY_LDT (descr, 0) : NULL)
182 # else
183 # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
184 TLS_DO_MODIFY_LDT ((descr), 0)
185 # endif
187 #if defined NEED_DL_SYSINFO
188 # define INIT_SYSINFO \
189 head->sysinfo = GLRO(dl_sysinfo)
190 #else
191 # define INIT_SYSINFO
192 #endif
194 /* Code to initially initialize the thread pointer. This might need
195 special attention since 'errno' is not yet available and if the
196 operation can cause a failure 'errno' must not be touched.
198 The value of this macro is null if successful, or an error string. */
199 # define TLS_INIT_TP(descr, secondcall) \
200 ({ \
201 void *_descr = (descr); \
202 tcbhead_t *head = _descr; \
204 head->tcb = _descr; \
205 /* For now the thread descriptor is at the same address. */ \
206 head->self = _descr; \
208 INIT_SYSINFO; \
209 TLS_SETUP_GS_SEGMENT (_descr, secondcall); \
212 /* Indicate that dynamic linker shouldn't try to initialize TLS even
213 when no PT_TLS segments are found in the program and libraries
214 it is linked against. */
215 # define TLS_INIT_TP_EXPENSIVE 1
217 /* Return the address of the dtv for the current thread. */
218 # define THREAD_DTV() \
219 ({ struct _pthread_descr_struct *__descr; \
220 THREAD_GETMEM (__descr, p_header.data.dtvp); })
222 # endif /* HAVE_TLS_SUPPORT && (FLOATING_STACKS || !IS_IN_libpthread) */
223 #endif /* __ASSEMBLER__ */
225 #endif /* tls.h */