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(). */
28 /* We don't want to include the kernel header. So duplicate the
31 /* Structure passed on `modify_ldt' call. */
32 struct modify_ldt_ldt_s
34 unsigned int entry_number;
35 unsigned long int base_addr;
37 unsigned int seg_32bit:1;
38 unsigned int contents:2;
39 unsigned int read_exec_only:1;
40 unsigned int limit_in_pages:1;
41 unsigned int seg_not_present:1;
42 unsigned int useable:1;
43 unsigned int empty:25;
46 /* System call to set LDT entry. */
47 extern int __modify_ldt (int, struct modify_ldt_ldt_s *, size_t);
50 /* Return the thread descriptor for the current thread.
52 The contained asm must *not* be marked volatile since otherwise
54 pthread_descr self = thread_self();
55 do not get optimized away. */
58 register pthread_descr __self; \
59 __asm__ ("movl %%gs:%c1,%0" : "=r" (__self) \
60 : "i" (offsetof (struct _pthread_descr_struct, \
61 p_header.data.self))); \
66 /* Initialize the thread-unique value. Two possible ways to do it. */
68 #define DO_MODIFY_LDT(descr, nr) \
70 struct modify_ldt_ldt_s ldt_entry = \
71 { nr, (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
72 1, 0, 0, 1, 0, 1, 0 }; \
73 if (__modify_ldt (1, &ldt_entry, sizeof (ldt_entry)) != 0) \
75 asm ("movw %w0, %%gs" : : "q" (nr * 8 + 7)); \
79 # define USETLS_EBX_ARG "r"
80 # define USETLS_LOAD_EBX "xchgl %1, %%ebx\n\t"
82 # define USETLS_EBX_ARG "b"
83 # define USETLS_LOAD_EBX
86 /* When using the new set_thread_area call, we don't need to change %gs
87 because we inherited the value set up in the main thread by TLS setup.
88 We need to extract that value and set up the same segment in this
91 # define DO_SET_THREAD_AREA_REUSE(nr) 1
93 /* Without TLS, we do the initialization of the main thread, where NR == 0. */
94 # define DO_SET_THREAD_AREA_REUSE(nr) (!__builtin_constant_p (nr) || (nr))
96 #define DO_SET_THREAD_AREA(descr, nr) \
99 if (DO_SET_THREAD_AREA_REUSE (nr)) \
101 asm ("movw %%gs, %w0" : "=q" (__gs)); \
102 struct modify_ldt_ldt_s ldt_entry = \
103 { (__gs & 0xffff) >> 3, \
104 (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
105 1, 0, 0, 1, 0, 1, 0 }; \
108 __asm (USETLS_LOAD_EBX \
109 "movl %2, %%eax\n\t" \
113 : USETLS_EBX_ARG (&ldt_entry), "i" (__NR_set_thread_area), \
117 asm ("movw %w0, %%gs" :: "q" (__gs)); \
123 struct modify_ldt_ldt_s ldt_entry = \
125 (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
126 1, 0, 0, 1, 0, 1, 0 }; \
128 __asm (USETLS_LOAD_EBX \
129 "movl %2, %%eax\n\t" \
133 : USETLS_EBX_ARG (&ldt_entry), "i" (__NR_set_thread_area), \
138 __gs = (ldt_entry.entry_number << 3) + 3; \
139 asm ("movw %w0, %%gs" : : "q" (__gs)); \
147 #if defined __ASSUME_SET_THREAD_AREA_SYSCALL
148 # define INIT_THREAD_SELF(descr, nr) DO_SET_THREAD_AREA (descr, nr)
149 #elif defined __NR_set_thread_area
150 # define INIT_THREAD_SELF(descr, nr) \
152 if (__builtin_expect (__have_no_set_thread_area, 0) \
153 || (DO_SET_THREAD_AREA (descr, DO_SET_THREAD_AREA_REUSE (nr)) == -1 \
154 && (__have_no_set_thread_area = 1))) \
155 DO_MODIFY_LDT (descr, nr); \
157 /* Defined in pspinlock.c. */
158 extern int __have_no_set_thread_area;
160 # define INIT_THREAD_SELF(descr, nr) DO_MODIFY_LDT (descr, nr)
163 /* Free resources associated with thread descriptor. */
164 #ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
165 #define FREE_THREAD(descr, nr) do { } while (0)
166 #elif defined __NR_set_thread_area
167 #define FREE_THREAD(descr, nr) \
170 __asm__ __volatile__ ("movw %%gs, %w0" : "=q" (__gs)); \
171 if (__builtin_expect (__gs & 4, 0)) \
173 struct modify_ldt_ldt_s ldt_entry = \
174 { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
175 __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
179 #define FREE_THREAD(descr, nr) \
181 struct modify_ldt_ldt_s ldt_entry = \
182 { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
183 __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
187 /* Read member of the thread descriptor directly. */
188 #define THREAD_GETMEM(descr, member) \
190 __typeof__ (descr->member) __value; \
191 if (sizeof (__value) == 1) \
192 __asm__ __volatile__ ("movb %%gs:%P2,%b0" \
195 "i" (offsetof (struct _pthread_descr_struct, \
197 else if (sizeof (__value) == 4) \
198 __asm__ __volatile__ ("movl %%gs:%P1,%0" \
200 : "i" (offsetof (struct _pthread_descr_struct, \
204 if (sizeof (__value) != 8) \
205 /* There should not be any value with a size other than 1, 4 or 8. */\
208 __asm__ __volatile__ ("movl %%gs:%P1,%%eax\n\t" \
209 "movl %%gs:%P2,%%edx" \
211 : "i" (offsetof (struct _pthread_descr_struct, \
213 "i" (offsetof (struct _pthread_descr_struct, \
219 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
220 #define THREAD_GETMEM_NC(descr, member) \
222 __typeof__ (descr->member) __value; \
223 if (sizeof (__value) == 1) \
224 __asm__ __volatile__ ("movb %%gs:(%2),%b0" \
227 "r" (offsetof (struct _pthread_descr_struct, \
229 else if (sizeof (__value) == 4) \
230 __asm__ __volatile__ ("movl %%gs:(%1),%0" \
232 : "r" (offsetof (struct _pthread_descr_struct, \
236 if (sizeof (__value) != 8) \
237 /* There should not be any value with a size other than 1, 4 or 8. */\
240 __asm__ __volatile__ ("movl %%gs:(%1),%%eax\n\t" \
241 "movl %%gs:4(%1),%%edx" \
243 : "r" (offsetof (struct _pthread_descr_struct, \
249 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
250 #define THREAD_SETMEM(descr, member, value) \
252 __typeof__ (descr->member) __value = (value); \
253 if (sizeof (__value) == 1) \
254 __asm__ __volatile__ ("movb %0,%%gs:%P1" : \
256 "i" (offsetof (struct _pthread_descr_struct, \
258 else if (sizeof (__value) == 4) \
259 __asm__ __volatile__ ("movl %0,%%gs:%P1" : \
261 "i" (offsetof (struct _pthread_descr_struct, \
265 if (sizeof (__value) != 8) \
266 /* There should not be any value with a size other than 1, 4 or 8. */\
269 __asm__ __volatile__ ("movl %%eax,%%gs:%P1\n\n" \
270 "movl %%edx,%%gs:%P2" : \
272 "i" (offsetof (struct _pthread_descr_struct, \
274 "i" (offsetof (struct _pthread_descr_struct, \
279 /* Set member of the thread descriptor directly. */
280 #define THREAD_SETMEM_NC(descr, member, value) \
282 __typeof__ (descr->member) __value = (value); \
283 if (sizeof (__value) == 1) \
284 __asm__ __volatile__ ("movb %0,%%gs:(%1)" : \
286 "r" (offsetof (struct _pthread_descr_struct, \
288 else if (sizeof (__value) == 4) \
289 __asm__ __volatile__ ("movl %0,%%gs:(%1)" : \
291 "r" (offsetof (struct _pthread_descr_struct, \
295 if (sizeof (__value) != 8) \
296 /* There should not be any value with a size other than 1, 4 or 8. */\
299 __asm__ __volatile__ ("movl %%eax,%%gs:(%1)\n\t" \
300 "movl %%edx,%%gs:4(%1)" : \
302 "r" (offsetof (struct _pthread_descr_struct, \
308 #if __ASSUME_LDT_WORKS > 0
309 /* We want the OS to assign stack addresses. */
310 #define FLOATING_STACKS 1
312 /* Maximum size of the stack if the rlimit is unlimited. */
313 #define ARCH_STACK_MAX_SIZE 8*1024*1024