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