1 /* Machine-dependent pthreads configuration and inline functions.
3 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #define _PT_MACHINE_H 1
25 # include <stddef.h> /* For offsetof. */
26 # include <stdlib.h> /* For abort(). */
27 # include <asm/prctl.h>
31 # define PT_EI extern inline __attribute__ ((always_inline))
34 extern long int testandset (int *spinlock
);
35 extern int __compare_and_swap (long int *p
, long int oldval
, long int newval
);
37 /* Get some notion of the current stack. Need not be exactly the top
38 of the stack, just something somewhere in the current frame. */
39 # define CURRENT_STACK_FRAME stack_pointer
40 register char * stack_pointer
__asm__ ("%rsp") __attribute_used__
;
43 /* Spinlock implementation; required. */
45 testandset (int *spinlock
)
49 __asm__
__volatile__ (
51 : "=r"(ret
), "=m"(*spinlock
)
52 : "0"(1), "m"(*spinlock
)
59 /* Compare-and-swap for semaphores. */
60 # define HAS_COMPARE_AND_SWAP
63 __compare_and_swap (long int *p
, long int oldval
, long int newval
)
68 __asm__
__volatile__ ("lock; cmpxchgq %3, %1; sete %0"
69 : "=q" (ret
), "=m" (*p
), "=a" (readval
)
70 : "r" (newval
), "m" (*p
), "a" (oldval
)
75 /* Return the thread descriptor for the current thread.
77 The contained asm must *not* be marked volatile since otherwise
79 pthread_descr self = thread_self();
80 do not get optimized away. */
81 # define THREAD_SELF \
83 register pthread_descr __self; \
84 __asm__ ("movq %%fs:%c1,%0" : "=r" (__self) \
85 : "i" (offsetof (struct _pthread_descr_struct, \
86 p_header.data.self))); \
90 /* Prototype for the system call. */
91 extern int __arch_prctl (int __code
, unsigned long __addr
);
93 /* Initialize the thread-unique value. */
94 # define INIT_THREAD_SELF(descr, nr) \
96 if (__arch_prctl (ARCH_SET_FS, (unsigned long)descr) != 0) \
100 /* Read member of the thread descriptor directly. */
101 # define THREAD_GETMEM(descr, member) \
103 __typeof__ (descr->member) __value; \
104 if (sizeof (__value) == 1) \
105 __asm__ __volatile__ ("movb %%fs:%P2,%b0" \
108 "i" (offsetof (struct _pthread_descr_struct, \
110 else if (sizeof (__value) == 4) \
111 __asm__ __volatile__ ("movl %%fs:%P2,%k0" \
114 "i" (offsetof (struct _pthread_descr_struct, \
118 if (sizeof (__value) != 8) \
119 /* There should not be any value with a size other than 1, 4 or 8. */\
122 __asm__ __volatile__ ("movq %%fs:%P1,%0" \
124 : "i" (offsetof (struct _pthread_descr_struct, \
130 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
131 # define THREAD_GETMEM_NC(descr, member) \
133 __typeof__ (descr->member) __value; \
134 if (sizeof (__value) == 1) \
135 __asm__ __volatile__ ("movb %%fs:(%2),%b0" \
138 "r" (offsetof (struct _pthread_descr_struct, \
140 else if (sizeof (__value) == 4) \
141 __asm__ __volatile__ ("movl %%fs:(%2),%k0" \
144 "r" (offsetof (struct _pthread_descr_struct, \
148 if (sizeof (__value) != 8) \
149 /* There should not be any value with a size other than 1, 4 or 8. */\
152 __asm__ __volatile__ ("movq %%fs:(%1),%0" \
154 : "r" (offsetof (struct _pthread_descr_struct, \
160 /* Set member of the thread descriptor directly. */
161 # define THREAD_SETMEM(descr, member, value) \
163 __typeof__ (descr->member) __value = (value); \
164 if (sizeof (__value) == 1) \
165 __asm__ __volatile__ ("movb %0,%%fs:%P1" : \
167 "i" (offsetof (struct _pthread_descr_struct, \
169 else if (sizeof (__value) == 4) \
170 __asm__ __volatile__ ("movl %k0,%%fs:%P1" : \
172 "i" (offsetof (struct _pthread_descr_struct, \
176 if (sizeof (__value) != 8) \
177 /* There should not be any value with a size other than 1, 4 or 8. */\
180 __asm__ __volatile__ ("movq %0,%%fs:%P1" : \
182 "i" (offsetof (struct _pthread_descr_struct, \
187 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
188 # define THREAD_SETMEM_NC(descr, member, value) \
190 __typeof__ (descr->member) __value = (value); \
191 if (sizeof (__value) == 1) \
192 __asm__ __volatile__ ("movb %0,%%fs:(%1)" : \
194 "r" (offsetof (struct _pthread_descr_struct, \
196 else if (sizeof (__value) == 4) \
197 __asm__ __volatile__ ("movl %k0,%%fs:(%1)" : \
199 "r" (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__ ("movq %0,%%fs:(%1)" : \
209 "r" (offsetof (struct _pthread_descr_struct, \
214 #endif /* !__ASSEMBLER__ */
216 /* We want the OS to assign stack addresses. */
217 #define FLOATING_STACKS 1
219 /* Maximum size of the stack if the rlimit is unlimited. */
220 #define ARCH_STACK_MAX_SIZE 32*1024*1024
222 /* The ia32e really want some help to prevent overheating. */
223 #define BUSY_WAIT_NOP __asm__ ("rep; nop")
225 #endif /* pt-machine.h */