chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / linuxthreads / sysdeps / x86_64 / pt-machine.h
1 /* Machine-dependent pthreads configuration and inline functions.
2    x86-64 version.
3    Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
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.
10
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.
15
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
19    02111-1307 USA.  */
20
21 #ifndef _PT_MACHINE_H
22 #define _PT_MACHINE_H   1
23
24 #ifndef __ASSEMBLER__
25 # include <stddef.h>    /* For offsetof.  */
26 # include <stdlib.h>    /* For abort().  */
27 # include <asm/prctl.h>
28
29
30 # ifndef PT_EI
31 #  define PT_EI extern inline __attribute__ ((always_inline))
32 # endif
33
34 extern long int testandset (int *spinlock);
35 extern int __compare_and_swap (long int *p, long int oldval, long int newval);
36
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__;
41
42
43 /* Spinlock implementation; required.  */
44 PT_EI long int
45 testandset (int *spinlock)
46 {
47   long int ret;
48
49   __asm__ __volatile__ (
50         "xchgl %k0, %1"
51         : "=r"(ret), "=m"(*spinlock)
52         : "0"(1), "m"(*spinlock)
53         : "memory");
54
55   return ret;
56 }
57
58
59 /* Compare-and-swap for semaphores.  */
60 # define HAS_COMPARE_AND_SWAP
61
62 PT_EI int
63 __compare_and_swap (long int *p, long int oldval, long int newval)
64 {
65   char ret;
66   long int readval;
67
68   __asm__ __volatile__ ("lock; cmpxchgq %3, %1; sete %0"
69                         : "=q" (ret), "=m" (*p), "=a" (readval)
70                         : "r" (newval), "m" (*p), "a" (oldval)
71                         : "memory");
72   return ret;
73 }
74
75 /* Return the thread descriptor for the current thread.
76
77    The contained asm must *not* be marked volatile since otherwise
78    assignments like
79         pthread_descr self = thread_self();
80    do not get optimized away.  */
81 # define THREAD_SELF \
82 ({                                                                            \
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)));                            \
87   __self;                                                                     \
88 })
89
90 /* Prototype for the system call.  */
91 extern int __arch_prctl (int __code, unsigned long __addr);
92
93 /* Initialize the thread-unique value.  */
94 # define INIT_THREAD_SELF(descr, nr) \
95 {                                                                             \
96   if (__arch_prctl (ARCH_SET_FS, (unsigned long)descr) != 0)                  \
97     abort ();                                                                 \
98 }
99
100 /* Read member of the thread descriptor directly.  */
101 # define THREAD_GETMEM(descr, member) \
102 ({                                                                            \
103   __typeof__ (descr->member) __value;                                         \
104   if (sizeof (__value) == 1)                                                  \
105     __asm__ __volatile__ ("movb %%fs:%P2,%b0"                                 \
106                           : "=q" (__value)                                    \
107                           : "0" (0),                                          \
108                             "i" (offsetof (struct _pthread_descr_struct,      \
109                                            member)));                         \
110   else if (sizeof (__value) == 4)                                             \
111     __asm__ __volatile__ ("movl %%fs:%P2,%k0"                                 \
112                           : "=r" (__value)                                    \
113                           : "0" (0),                                          \
114                             "i" (offsetof (struct _pthread_descr_struct,      \
115                                            member)));                         \
116   else                                                                        \
117     {                                                                         \
118       if (sizeof (__value) != 8)                                              \
119         /* There should not be any value with a size other than 1, 4 or 8.  */\
120         abort ();                                                             \
121                                                                               \
122       __asm__ __volatile__ ("movq %%fs:%P1,%0"                                \
123                             : "=r" (__value)                                  \
124                             : "i" (offsetof (struct _pthread_descr_struct,    \
125                                              member)));                       \
126     }                                                                         \
127   __value;                                                                    \
128 })
129
130 /* Same as THREAD_GETMEM, but the member offset can be non-constant.  */
131 # define THREAD_GETMEM_NC(descr, member) \
132 ({                                                                            \
133   __typeof__ (descr->member) __value;                                         \
134   if (sizeof (__value) == 1)                                                  \
135     __asm__ __volatile__ ("movb %%fs:(%2),%b0"                                \
136                           : "=q" (__value)                                    \
137                           : "0" (0),                                          \
138                             "r" (offsetof (struct _pthread_descr_struct,      \
139                                            member)));                         \
140   else if (sizeof (__value) == 4)                                             \
141     __asm__ __volatile__ ("movl %%fs:(%2),%k0"                                \
142                           : "=r" (__value)                                    \
143                           : "0" (0),                                          \
144                             "r" (offsetof (struct _pthread_descr_struct,      \
145                                            member)));                         \
146   else                                                                        \
147     {                                                                         \
148       if (sizeof (__value) != 8)                                              \
149         /* There should not be any value with a size other than 1, 4 or 8.  */\
150         abort ();                                                             \
151                                                                               \
152       __asm__ __volatile__ ("movq %%fs:(%1),%0"                               \
153                             : "=r" (__value)                                  \
154                             : "r" (offsetof (struct _pthread_descr_struct,    \
155                                              member)));                       \
156     }                                                                         \
157   __value;                                                                    \
158 })
159
160 /* Set member of the thread descriptor directly.  */
161 # define THREAD_SETMEM(descr, member, value) \
162 ({                                                                            \
163   __typeof__ (descr->member) __value = (value);                               \
164   if (sizeof (__value) == 1)                                                  \
165     __asm__ __volatile__ ("movb %0,%%fs:%P1" :                                \
166                           : "q" (__value),                                    \
167                             "i" (offsetof (struct _pthread_descr_struct,      \
168                                            member)));                         \
169   else if (sizeof (__value) == 4)                                             \
170     __asm__ __volatile__ ("movl %k0,%%fs:%P1" :                               \
171                           : "r" (__value),                                    \
172                             "i" (offsetof (struct _pthread_descr_struct,      \
173                                            member)));                         \
174   else                                                                        \
175     {                                                                         \
176       if (sizeof (__value) != 8)                                              \
177         /* There should not be any value with a size other than 1, 4 or 8.  */\
178         abort ();                                                             \
179                                                                               \
180       __asm__ __volatile__ ("movq %0,%%fs:%P1" :                              \
181                             : "r" (__value),                                  \
182                               "i" (offsetof (struct _pthread_descr_struct,    \
183                                              member)));                       \
184     }                                                                         \
185 })
186
187 /* Same as THREAD_SETMEM, but the member offset can be non-constant.  */
188 # define THREAD_SETMEM_NC(descr, member, value) \
189 ({                                                                            \
190   __typeof__ (descr->member) __value = (value);                               \
191   if (sizeof (__value) == 1)                                                  \
192     __asm__ __volatile__ ("movb %0,%%fs:(%1)" :                               \
193                           : "q" (__value),                                    \
194                             "r" (offsetof (struct _pthread_descr_struct,      \
195                                            member)));                         \
196   else if (sizeof (__value) == 4)                                             \
197     __asm__ __volatile__ ("movl %k0,%%fs:(%1)" :                              \
198                           : "r" (__value),                                    \
199                             "r" (offsetof (struct _pthread_descr_struct,      \
200                                            member)));                         \
201   else                                                                        \
202     {                                                                         \
203       if (sizeof (__value) != 8)                                              \
204         /* There should not be any value with a size other than 1, 4 or 8.  */\
205         abort ();                                                             \
206                                                                               \
207       __asm__ __volatile__ ("movq %0,%%fs:(%1)" :                             \
208                             : "r" (__value),                                  \
209                               "r" (offsetof (struct _pthread_descr_struct,    \
210                                              member)));                       \
211     }                                                                         \
212 })
213
214 #endif /* !__ASSEMBLER__ */
215
216 /* We want the OS to assign stack addresses.  */
217 #define FLOATING_STACKS 1
218
219 /* Maximum size of the stack if the rlimit is unlimited.  */
220 #define ARCH_STACK_MAX_SIZE     32*1024*1024
221
222 /* The ia32e really want some help to prevent overheating.  */
223 #define BUSY_WAIT_NOP   __asm__ ("rep; nop")
224
225 #endif /* pt-machine.h */