1 /* Special definitions for ix86 machine using segment register based
3 Copyright (C) 1998, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include <stddef.h> /* For offsetof. */
27 #include <stdlib.h> /* For abort(). */
31 /* We don't want to include the kernel header. So duplicate the
34 /* Structure passed on `modify_ldt' call. */
35 struct modify_ldt_ldt_s
37 unsigned int entry_number
;
38 unsigned long int base_addr
;
40 unsigned int seg_32bit
:1;
41 unsigned int contents
:2;
42 unsigned int read_exec_only
:1;
43 unsigned int limit_in_pages
:1;
44 unsigned int seg_not_present
:1;
45 unsigned int useable
:1;
46 unsigned int empty
:25;
49 /* System call to set LDT entry. */
50 extern int __modify_ldt (int, struct modify_ldt_ldt_s
*, size_t);
53 /* Return the thread descriptor for the current thread.
55 The contained asm must *not* be marked volatile since otherwise
57 pthread_descr self = thread_self();
58 do not get optimized away. */
61 register pthread_descr __self; \
62 __asm__ ("movl %%gs:%c1,%0" : "=r" (__self) \
63 : "i" (offsetof (struct _pthread_descr_struct, \
64 p_header.data.self))); \
69 /* Initialize the thread-unique value. Two possible ways to do it. */
71 #define DO_MODIFY_LDT(descr, nr) \
73 struct modify_ldt_ldt_s ldt_entry = \
74 { nr, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
75 1, 0, 0, 1, 0, 1, 0 }; \
76 if (__modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) \
78 asm ("movw %w0, %%gs" : : "q" (nr * 8 + 7)); \
82 # define USETLS_EBX_ARG "r"
83 # define USETLS_LOAD_EBX "xchgl %1, %%ebx\n\t"
85 # define USETLS_EBX_ARG "b"
86 # define USETLS_LOAD_EBX
89 /* When using the new set_thread_area call, we don't need to change %gs
90 because we inherited the value set up in the main thread by TLS setup.
91 We need to extract that value and set up the same segment in this
94 # define DO_SET_THREAD_AREA_REUSE(nr) 1
96 /* Without TLS, we do the initialization of the main thread, where NR == 0. */
97 # define DO_SET_THREAD_AREA_REUSE(nr) (!__builtin_constant_p (nr) || (nr))
99 #define DO_SET_THREAD_AREA(descr, nr) \
102 if (DO_SET_THREAD_AREA_REUSE (nr)) \
104 asm ("movw %%gs, %w0" : "=q" (__gs)); \
105 struct modify_ldt_ldt_s ldt_entry = \
106 { (__gs & 0xffff) >> 3, \
107 (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
108 1, 0, 0, 1, 0, 1, 0 }; \
111 __asm (USETLS_LOAD_EBX \
112 "movl %2, %%eax\n\t" \
116 : USETLS_EBX_ARG (&ldt_entry), "i" (__NR_set_thread_area), \
120 asm ("movw %w0, %%gs" :: "q" (__gs)); \
126 struct modify_ldt_ldt_s ldt_entry = \
128 (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
129 1, 0, 0, 1, 0, 1, 0 }; \
131 __asm (USETLS_LOAD_EBX \
132 "movl %2, %%eax\n\t" \
136 : USETLS_EBX_ARG (&ldt_entry), "i" (__NR_set_thread_area), \
141 __gs = (ldt_entry.entry_number << 3) + 3; \
142 asm ("movw %w0, %%gs" : : "q" (__gs)); \
150 #if defined __ASSUME_SET_THREAD_AREA_SYSCALL
151 # define INIT_THREAD_SELF(descr, nr) DO_SET_THREAD_AREA (descr, nr)
152 #elif defined __NR_set_thread_area
153 # define INIT_THREAD_SELF(descr, nr) \
155 if (__builtin_expect (__have_no_set_thread_area, 0) \
156 || (DO_SET_THREAD_AREA (descr, DO_SET_THREAD_AREA_REUSE (nr)) == -1 \
157 && (__have_no_set_thread_area = 1))) \
158 DO_MODIFY_LDT (descr, nr); \
160 /* Defined in pspinlock.c. */
161 extern int __have_no_set_thread_area
;
163 # define INIT_THREAD_SELF(descr, nr) DO_MODIFY_LDT (descr, nr)
166 /* Free resources associated with thread descriptor. */
167 #ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
168 #define FREE_THREAD(descr, nr) do { } while (0)
169 #elif defined __NR_set_thread_area
170 #define FREE_THREAD(descr, nr) \
173 __asm__ __volatile__ ("movw %%gs, %w0" : "=q" (__gs)); \
174 if (__builtin_expect (__gs & 4, 0)) \
176 struct modify_ldt_ldt_s ldt_entry = \
177 { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
178 __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
182 #define FREE_THREAD(descr, nr) \
184 struct modify_ldt_ldt_s ldt_entry = \
185 { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
186 __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
190 /* Read member of the thread descriptor directly. */
191 #define THREAD_GETMEM(descr, member) \
193 __typeof__ (descr->member) __value; \
194 if (sizeof (__value) == 1) \
195 __asm__ __volatile__ ("movb %%gs:%P2,%b0" \
198 "i" (offsetof (struct _pthread_descr_struct, \
200 else if (sizeof (__value) == 4) \
201 __asm__ __volatile__ ("movl %%gs:%P1,%0" \
203 : "i" (offsetof (struct _pthread_descr_struct, \
207 if (sizeof (__value) != 8) \
208 /* There should not be any value with a size other than 1, 4 or 8. */\
211 __asm__ __volatile__ ("movl %%gs:%P1,%%eax\n\t" \
212 "movl %%gs:%P2,%%edx" \
214 : "i" (offsetof (struct _pthread_descr_struct, \
216 "i" (offsetof (struct _pthread_descr_struct, \
222 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
223 #define THREAD_GETMEM_NC(descr, member) \
225 __typeof__ (descr->member) __value; \
226 if (sizeof (__value) == 1) \
227 __asm__ __volatile__ ("movb %%gs:(%2),%b0" \
230 "r" (offsetof (struct _pthread_descr_struct, \
232 else if (sizeof (__value) == 4) \
233 __asm__ __volatile__ ("movl %%gs:(%1),%0" \
235 : "r" (offsetof (struct _pthread_descr_struct, \
239 if (sizeof (__value) != 8) \
240 /* There should not be any value with a size other than 1, 4 or 8. */\
243 __asm__ __volatile__ ("movl %%gs:(%1),%%eax\n\t" \
244 "movl %%gs:4(%1),%%edx" \
246 : "r" (offsetof (struct _pthread_descr_struct, \
252 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
253 #define THREAD_SETMEM(descr, member, value) \
255 __typeof__ (descr->member) __value = (value); \
256 if (sizeof (__value) == 1) \
257 __asm__ __volatile__ ("movb %0,%%gs:%P1" : \
259 "i" (offsetof (struct _pthread_descr_struct, \
261 else if (sizeof (__value) == 4) \
262 __asm__ __volatile__ ("movl %0,%%gs:%P1" : \
264 "i" (offsetof (struct _pthread_descr_struct, \
268 if (sizeof (__value) != 8) \
269 /* There should not be any value with a size other than 1, 4 or 8. */\
272 __asm__ __volatile__ ("movl %%eax,%%gs:%P1\n\n" \
273 "movl %%edx,%%gs:%P2" : \
275 "i" (offsetof (struct _pthread_descr_struct, \
277 "i" (offsetof (struct _pthread_descr_struct, \
282 /* Set member of the thread descriptor directly. */
283 #define THREAD_SETMEM_NC(descr, member, value) \
285 __typeof__ (descr->member) __value = (value); \
286 if (sizeof (__value) == 1) \
287 __asm__ __volatile__ ("movb %0,%%gs:(%1)" : \
289 "r" (offsetof (struct _pthread_descr_struct, \
291 else if (sizeof (__value) == 4) \
292 __asm__ __volatile__ ("movl %0,%%gs:(%1)" : \
294 "r" (offsetof (struct _pthread_descr_struct, \
298 if (sizeof (__value) != 8) \
299 /* There should not be any value with a size other than 1, 4 or 8. */\
302 __asm__ __volatile__ ("movl %%eax,%%gs:(%1)\n\t" \
303 "movl %%edx,%%gs:4(%1)" : \
305 "r" (offsetof (struct _pthread_descr_struct, \
311 #if __ASSUME_LDT_WORKS > 0
312 /* We want the OS to assign stack addresses. */
313 #define FLOATING_STACKS 1
315 /* Maximum size of the stack if the rlimit is unlimited. */
316 #define ARCH_STACK_MAX_SIZE 8*1024*1024