1 /* Special definitions for ix86 machine using segment register based
3 Copyright (C) 1998, 2000, 2001, 2002 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. */
23 #include <stddef.h> /* For offsetof. */
24 #include <stdlib.h> /* For abort(). */
27 /* We don't want to include the kernel header. So duplicate the
30 /* Structure passed on `modify_ldt' call. */
31 struct modify_ldt_ldt_s
33 unsigned int entry_number;
34 unsigned long int base_addr;
36 unsigned int seg_32bit:1;
37 unsigned int contents:2;
38 unsigned int read_exec_only:1;
39 unsigned int limit_in_pages:1;
40 unsigned int seg_not_present:1;
41 unsigned int useable:1;
42 unsigned int empty:25;
45 /* System call to set LDT entry. */
46 extern int __modify_ldt (int, struct modify_ldt_ldt_s *, size_t);
49 /* Return the thread descriptor for the current thread.
51 The contained asm must *not* be marked volatile since otherwise
53 pthread_descr self = thread_self();
54 do not get optimized away. */
57 register pthread_descr __self; \
58 __asm__ ("movl %%gs:%c1,%0" : "=r" (__self) \
59 : "i" (offsetof (struct _pthread_descr_struct, \
60 p_header.data.self))); \
65 /* Initialize the thread-unique value. Two possible ways to do it. */
67 #define DO_MODIFY_LDT(descr, nr) \
69 struct modify_ldt_ldt_s ldt_entry = \
70 { nr, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
71 1, 0, 0, 1, 0, 1, 0 }; \
72 if (__modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) \
74 asm ("movw %w0, %%gs" : : "q" (nr * 8 + 7)); \
78 # define USETLS_EBX_ARG "r"
79 # define USETLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
81 # define USETLS_EBX_ARG "b"
82 # define USETLS_LOAD_EBX
85 /* When using the new set_thread_area call, we don't need to change %gs
86 because we inherited the value set up in the main thread by TLS setup.
87 We need to extract that value and set up the same segment in this
90 # define DO_SET_THREAD_AREA_REUSE(nr) 1
92 /* Without TLS, we do the initialization of the main thread, where NR == 0. */
93 # define DO_SET_THREAD_AREA_REUSE(nr) (!__builtin_constant_p (nr) || (nr))
95 #define DO_SET_THREAD_AREA(descr, nr) \
98 if (DO_SET_THREAD_AREA_REUSE (nr)) \
100 asm ("movw %%gs, %w0" : "=q" (__gs)); \
101 struct modify_ldt_ldt_s ldt_entry = \
102 { (__gs & 0xffff) >> 3, \
103 (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
104 1, 0, 0, 1, 0, 1, 0 }; \
107 __asm (USETLS_LOAD_EBX \
108 "movl %2, %%eax\n\t" \
112 : USETLS_EBX_ARG (&ldt_entry), "i" (__NR_set_thread_area)); \
114 asm ("movw %w0, %%gs" :: "q" (__gs)); \
120 struct modify_ldt_ldt_s ldt_entry = \
122 (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
123 1, 0, 0, 1, 0, 1, 0 }; \
125 __asm (USETLS_LOAD_EBX \
126 "movl %2, %%eax\n\t" \
130 : USETLS_EBX_ARG (&ldt_entry), "i" (__NR_set_thread_area)); \
133 __gs = (ldt_entry.entry_number << 3) + 3; \
134 asm ("movw %w0, %%gs" : : "q" (__gs)); \
142 #if defined __ASSUME_SET_THREAD_AREA_SYSCALL
143 # define INIT_THREAD_SELF(descr, nr) DO_SET_THREAD_AREA (descr, nr)
144 #elif defined __NR_set_thread_area
145 # define INIT_THREAD_SELF(descr, nr) \
147 if (__builtin_expect (__have_no_set_thread_area, 0) \
148 || (DO_SET_THREAD_AREA (descr, DO_SET_THREAD_AREA_REUSE (nr)) == -1 \
149 && (__have_no_set_thread_area = 1))) \
150 DO_MODIFY_LDT (descr, nr); \
152 /* Defined in pspinlock.c. */
153 extern int __have_no_set_thread_area;
155 # define INIT_THREAD_SELF(descr, nr) DO_MODIFY_LDT (descr, nr)
158 /* Free resources associated with thread descriptor. */
159 #ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
160 #define FREE_THREAD(descr, nr) do { } while (0)
161 #elif defined __NR_set_thread_area
162 #define FREE_THREAD(descr, nr) \
165 __asm__ __volatile__ ("movw %%gs, %w0" : "=q" (__gs)); \
166 if (__builtin_expect (__gs & 4, 0)) \
168 struct modify_ldt_ldt_s ldt_entry = \
169 { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
170 __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
174 #define FREE_THREAD(descr, nr) \
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 /* Read member of the thread descriptor directly. */
183 #define THREAD_GETMEM(descr, member) \
185 __typeof__ (descr->member) __value; \
186 if (sizeof (__value) == 1) \
187 __asm__ __volatile__ ("movb %%gs:%P2,%b0" \
190 "i" (offsetof (struct _pthread_descr_struct, \
192 else if (sizeof (__value) == 4) \
193 __asm__ __volatile__ ("movl %%gs:%P1,%0" \
195 : "i" (offsetof (struct _pthread_descr_struct, \
199 if (sizeof (__value) != 8) \
200 /* There should not be any value with a size other than 1, 4 or 8. */\
203 __asm__ __volatile__ ("movl %%gs:%P1,%%eax\n\t" \
204 "movl %%gs:%P2,%%edx" \
206 : "i" (offsetof (struct _pthread_descr_struct, \
208 "i" (offsetof (struct _pthread_descr_struct, \
214 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
215 #define THREAD_GETMEM_NC(descr, member) \
217 __typeof__ (descr->member) __value; \
218 if (sizeof (__value) == 1) \
219 __asm__ __volatile__ ("movb %%gs:(%2),%b0" \
222 "r" (offsetof (struct _pthread_descr_struct, \
224 else if (sizeof (__value) == 4) \
225 __asm__ __volatile__ ("movl %%gs:(%1),%0" \
227 : "r" (offsetof (struct _pthread_descr_struct, \
231 if (sizeof (__value) != 8) \
232 /* There should not be any value with a size other than 1, 4 or 8. */\
235 __asm__ __volatile__ ("movl %%gs:(%1),%%eax\n\t" \
236 "movl %%gs:4(%1),%%edx" \
238 : "r" (offsetof (struct _pthread_descr_struct, \
244 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
245 #define THREAD_SETMEM(descr, member, value) \
247 __typeof__ (descr->member) __value = (value); \
248 if (sizeof (__value) == 1) \
249 __asm__ __volatile__ ("movb %0,%%gs:%P1" : \
251 "i" (offsetof (struct _pthread_descr_struct, \
253 else if (sizeof (__value) == 4) \
254 __asm__ __volatile__ ("movl %0,%%gs:%P1" : \
256 "i" (offsetof (struct _pthread_descr_struct, \
260 if (sizeof (__value) != 8) \
261 /* There should not be any value with a size other than 1, 4 or 8. */\
264 __asm__ __volatile__ ("movl %%eax,%%gs:%P1\n\n" \
265 "movl %%edx,%%gs:%P2" : \
267 "i" (offsetof (struct _pthread_descr_struct, \
269 "i" (offsetof (struct _pthread_descr_struct, \
274 /* Set member of the thread descriptor directly. */
275 #define THREAD_SETMEM_NC(descr, member, value) \
277 __typeof__ (descr->member) __value = (value); \
278 if (sizeof (__value) == 1) \
279 __asm__ __volatile__ ("movb %0,%%gs:(%1)" : \
281 "r" (offsetof (struct _pthread_descr_struct, \
283 else if (sizeof (__value) == 4) \
284 __asm__ __volatile__ ("movl %0,%%gs:(%1)" : \
286 "r" (offsetof (struct _pthread_descr_struct, \
290 if (sizeof (__value) != 8) \
291 /* There should not be any value with a size other than 1, 4 or 8. */\
294 __asm__ __volatile__ ("movl %%eax,%%gs:(%1)\n\t" \
295 "movl %%edx,%%gs:4(%1)" : \
297 "r" (offsetof (struct _pthread_descr_struct, \
303 /* We want the OS to assign stack addresses. */
304 #define FLOATING_STACKS 1
306 /* Maximum size of the stack if the rlimit is unlimited. */
307 #define ARCH_STACK_MAX_SIZE 8*1024*1024