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 see <http://www.gnu.org/licenses/>. */
22 #include <stddef.h> /* For offsetof. */
23 #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 %1, %%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
89 #ifdef __UCLIBC_HAS_TLS__
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), \
116 __asm__ ("movw %w0, %%gs" :: "q" (__gs)); \
122 struct modify_ldt_ldt_s ldt_entry = \
124 (unsigned long int) (descr), 0xfffff /* 4GB in pages */, \
125 1, 0, 0, 1, 0, 1, 0 }; \
127 __asm__ (USETLS_LOAD_EBX \
128 "movl %2, %%eax\n\t" \
132 : USETLS_EBX_ARG (&ldt_entry), "i" (__NR_set_thread_area), \
137 __gs = (ldt_entry.entry_number << 3) + 3; \
138 __asm__ ("movw %w0, %%gs" : : "q" (__gs)); \
146 #if defined __ASSUME_SET_THREAD_AREA_SYSCALL
147 # define INIT_THREAD_SELF(descr, nr) DO_SET_THREAD_AREA (descr, nr)
148 #elif defined __NR_set_thread_area
149 # define INIT_THREAD_SELF(descr, nr) \
151 if (__builtin_expect (__have_no_set_thread_area, 0) \
152 || (DO_SET_THREAD_AREA (descr, DO_SET_THREAD_AREA_REUSE (nr)) == -1 \
153 && (__have_no_set_thread_area = 1))) \
154 DO_MODIFY_LDT (descr, nr); \
156 /* Defined in pspinlock.c. */
157 extern int __have_no_set_thread_area;
159 # define INIT_THREAD_SELF(descr, nr) DO_MODIFY_LDT (descr, nr)
162 /* Free resources associated with thread descriptor. */
163 #ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
164 #define FREE_THREAD(descr, nr) do { } while (0)
165 #elif defined __NR_set_thread_area
166 #define FREE_THREAD(descr, nr) \
169 __asm__ __volatile__ ("movw %%gs, %w0" : "=q" (__gs)); \
170 if (__builtin_expect (__gs & 4, 0)) \
172 struct modify_ldt_ldt_s ldt_entry = \
173 { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
174 __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
178 #define FREE_THREAD(descr, nr) \
180 struct modify_ldt_ldt_s ldt_entry = \
181 { nr, 0, 0, 0, 0, 1, 0, 1, 0, 0 }; \
182 __modify_ldt (1, &ldt_entry, sizeof (ldt_entry)); \
186 /* Read member of the thread descriptor directly. */
187 #define THREAD_GETMEM(descr, member) \
189 __typeof__ (descr->member) __value; \
190 if (sizeof (__value) == 1) \
191 __asm__ __volatile__ ("movb %%gs:%P2,%b0" \
194 "i" (offsetof (struct _pthread_descr_struct, \
196 else if (sizeof (__value) == 4) \
197 __asm__ __volatile__ ("movl %%gs:%P1,%0" \
199 : "i" (offsetof (struct _pthread_descr_struct, \
203 if (sizeof (__value) != 8) \
204 /* There should not be any value with a size other than 1, 4 or 8. */\
207 __asm__ __volatile__ ("movl %%gs:%P1,%%eax\n\t" \
208 "movl %%gs:%P2,%%edx" \
210 : "i" (offsetof (struct _pthread_descr_struct, \
212 "i" (offsetof (struct _pthread_descr_struct, \
218 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
219 #define THREAD_GETMEM_NC(descr, member) \
221 __typeof__ (descr->member) __value; \
222 if (sizeof (__value) == 1) \
223 __asm__ __volatile__ ("movb %%gs:(%2),%b0" \
226 "r" (offsetof (struct _pthread_descr_struct, \
228 else if (sizeof (__value) == 4) \
229 __asm__ __volatile__ ("movl %%gs:(%1),%0" \
231 : "r" (offsetof (struct _pthread_descr_struct, \
235 if (sizeof (__value) != 8) \
236 /* There should not be any value with a size other than 1, 4 or 8. */\
239 __asm__ __volatile__ ("movl %%gs:(%1),%%eax\n\t" \
240 "movl %%gs:4(%1),%%edx" \
242 : "r" (offsetof (struct _pthread_descr_struct, \
248 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
249 #define THREAD_SETMEM(descr, member, value) \
251 __typeof__ (descr->member) __value = (value); \
252 if (sizeof (__value) == 1) \
253 __asm__ __volatile__ ("movb %0,%%gs:%P1" : \
255 "i" (offsetof (struct _pthread_descr_struct, \
257 else if (sizeof (__value) == 4) \
258 __asm__ __volatile__ ("movl %0,%%gs:%P1" : \
260 "i" (offsetof (struct _pthread_descr_struct, \
264 if (sizeof (__value) != 8) \
265 /* There should not be any value with a size other than 1, 4 or 8. */\
268 __asm__ __volatile__ ("movl %%eax,%%gs:%P1\n\n" \
269 "movl %%edx,%%gs:%P2" : \
271 "i" (offsetof (struct _pthread_descr_struct, \
273 "i" (offsetof (struct _pthread_descr_struct, \
278 /* Set member of the thread descriptor directly. */
279 #define THREAD_SETMEM_NC(descr, member, value) \
281 __typeof__ (descr->member) __value = (value); \
282 if (sizeof (__value) == 1) \
283 __asm__ __volatile__ ("movb %0,%%gs:(%1)" : \
285 "r" (offsetof (struct _pthread_descr_struct, \
287 else if (sizeof (__value) == 4) \
288 __asm__ __volatile__ ("movl %0,%%gs:(%1)" : \
290 "r" (offsetof (struct _pthread_descr_struct, \
294 if (sizeof (__value) != 8) \
295 /* There should not be any value with a size other than 1, 4 or 8. */\
298 __asm__ __volatile__ ("movl %%eax,%%gs:(%1)\n\t" \
299 "movl %%edx,%%gs:4(%1)" : \
301 "r" (offsetof (struct _pthread_descr_struct, \
307 #if __ASSUME_LDT_WORKS > 0
308 /* We want the OS to assign stack addresses. */
309 #define FLOATING_STACKS 1
311 /* Maximum size of the stack if the rlimit is unlimited. */
312 #define ARCH_STACK_MAX_SIZE 8*1024*1024