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, see
17 <http://www.gnu.org/licenses/>. */
22 # include <pt-machine.h>
29 /* Type for the dtv. */
43 void *tcb; /* Pointer to the TCB. Not necessary the
44 thread descriptor used by libpthread. */
46 void *self; /* Pointer to the thread descriptor. */
51 #else /* __ASSEMBLER__ */
52 # include <tcb-offsets.h>
55 /* We can support TLS only if the floating-stack support is available.
56 However, we want to compile in the support and test at runtime whether
57 the running kernel can support it or not. To avoid bothering with the
58 TLS support code at all, use configure --without-tls.
60 We need USE_TLS to be consistently defined, for ldsodefs.h conditionals.
61 But some of the code below can cause problems in building libpthread
62 (e.g. useldt.h will defined FLOATING_STACKS when it shouldn't). */
64 #if defined HAVE_TLS_SUPPORT \
65 && (defined FLOATING_STACKS || !defined IS_IN_libpthread)
67 /* Signal that TLS support is available. */
70 # ifndef __ASSEMBLER__
71 /* Get system call information. */
75 /* Get the thread descriptor definition. */
76 # include <linuxthreads/descr.h>
78 /* This is the size of the initial TCB. */
79 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
81 /* Alignment requirements for the initial TCB. */
82 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
84 /* This is the size of the TCB. */
85 # define TLS_TCB_SIZE sizeof (struct _pthread_descr_struct)
87 /* Alignment requirements for the TCB. */
88 # define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
90 /* The TCB can have any size and the memory following the address the
91 thread pointer points to is unspecified. Allocate the TCB there. */
92 # define TLS_TCB_AT_TP 1
95 /* Install the dtv pointer. The pointer passed is to the element with
96 index -1 which contain the length. */
97 # define INSTALL_DTV(descr, dtvp) \
98 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
100 /* Install new dtv for current thread. */
101 # define INSTALL_NEW_DTV(dtv) \
102 ({ struct _pthread_descr_struct *__descr; \
103 THREAD_SETMEM (__descr, p_header.data.dtvp, (dtv)); })
105 /* Return dtv of given thread descriptor. */
106 # define GET_DTV(descr) \
107 (((tcbhead_t *) (descr))->dtv)
110 # define TLS_EBX_ARG "r"
111 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
113 # define TLS_EBX_ARG "b"
114 # define TLS_LOAD_EBX
117 # if !defined IS_IN_libpthread && !defined DO_MODIFY_LDT
118 # include "useldt.h" /* For the structure. */
120 # if __ASSUME_LDT_WORKS > 0
121 # define TLS_DO_MODIFY_LDT_KERNEL_CHECK(doit) (doit) /* Nothing to check. */
123 # define TLS_DO_MODIFY_LDT_KERNEL_CHECK(doit) \
124 (__builtin_expect (GLRO(dl_osversion) < 131939, 0) \
125 ? "kernel too old for thread-local storage support\n" \
129 # define TLS_DO_MODIFY_LDT(descr, nr) \
130 TLS_DO_MODIFY_LDT_KERNEL_CHECK( \
132 struct modify_ldt_ldt_s ldt_entry = \
133 { nr, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
134 1, 0, 0, 1, 0, 1, 0 }; \
136 __asm__ __volatile__ (TLS_LOAD_EBX \
140 : "0" (__NR_modify_ldt), \
141 /* The extra argument with the "m" constraint is necessary \
142 to let the compiler know that we are accessing LDT_ENTRY \
144 "m" (ldt_entry), TLS_EBX_ARG (1), "c" (&ldt_entry), \
145 "d" (sizeof (ldt_entry))); \
146 __builtin_expect (result, 0) == 0 \
147 ? ({ __asm__ ("movw %w0, %%gs" : : "q" ((nr) * 8 + 7)); NULL; }) \
148 : "cannot set up LDT for thread-local storage\n"; \
151 # define TLS_DO_SET_THREAD_AREA(descr, secondcall) \
153 struct modify_ldt_ldt_s ldt_entry = \
154 { -1, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
155 1, 0, 0, 1, 0, 1, 0 }; \
158 ldt_entry.entry_number = ({ int _gs; \
159 __asm__ ("movw %%gs, %w0" : "=q" (_gs)); \
160 (_gs & 0xffff) >> 3; }); \
161 __asm__ __volatile__ (TLS_LOAD_EBX \
164 : "=a" (result), "=m" (ldt_entry.entry_number) \
165 : "0" (__NR_set_thread_area), \
166 /* The extra argument with the "m" constraint is necessary \
167 to let the compiler know that we are accessing LDT_ENTRY \
169 TLS_EBX_ARG (&ldt_entry), "m" (ldt_entry)); \
170 if (__builtin_expect (result, 0) == 0) \
171 __asm__ ("movw %w0, %%gs" : : "q" (ldt_entry.entry_number * 8 + 3)); \
175 # ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
176 # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
177 (TLS_DO_SET_THREAD_AREA (descr, secondcall) \
178 ? "set_thread_area failed when setting up thread-local storage\n" : NULL)
179 # elif defined __NR_set_thread_area
180 # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
181 (TLS_DO_SET_THREAD_AREA (descr, secondcall) \
182 ? TLS_DO_MODIFY_LDT (descr, 0) : NULL)
184 # define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
185 TLS_DO_MODIFY_LDT ((descr), 0)
188 #if defined NEED_DL_SYSINFO
189 # define INIT_SYSINFO \
190 head->sysinfo = GLRO(dl_sysinfo)
192 # define INIT_SYSINFO
195 /* Code to initially initialize the thread pointer. This might need
196 special attention since 'errno' is not yet available and if the
197 operation can cause a failure 'errno' must not be touched.
199 The value of this macro is null if successful, or an error string. */
200 # define TLS_INIT_TP(descr, secondcall) \
202 void *_descr = (descr); \
203 tcbhead_t *head = _descr; \
205 head->tcb = _descr; \
206 /* For now the thread descriptor is at the same address. */ \
207 head->self = _descr; \
210 TLS_SETUP_GS_SEGMENT (_descr, secondcall); \
213 /* Indicate that dynamic linker shouldn't try to initialize TLS even
214 when no PT_TLS segments are found in the program and libraries
215 it is linked against. */
216 # define TLS_INIT_TP_EXPENSIVE 1
218 /* Return the address of the dtv for the current thread. */
219 # define THREAD_DTV() \
220 ({ struct _pthread_descr_struct *__descr; \
221 THREAD_GETMEM (__descr, p_header.data.dtvp); })
223 # endif /* HAVE_TLS_SUPPORT && (FLOATING_STACKS || !IS_IN_libpthread) */
224 #endif /* __ASSEMBLER__ */