Upload Tizen:Base source
[external/eglibc.git] / sysdeps / unix / sysv / linux / powerpc / powerpc32 / sysdep.h
1 /* Copyright (C) 1992,1997-2003,2004,2005,2006 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #ifndef _LINUX_POWERPC_SYSDEP_H
20 #define _LINUX_POWERPC_SYSDEP_H 1
21
22 #include <sysdeps/unix/powerpc/sysdep.h>
23 #include <tls.h>
24
25 /* Some systen calls got renamed over time, but retained the same semantics.
26    Handle them here so they can be catched by both C and assembler stubs in
27    glibc.  */
28
29 #ifdef __NR_pread64
30 # ifdef __NR_pread
31 #  error "__NR_pread and __NR_pread64 both defined???"
32 # endif
33 # define __NR_pread __NR_pread64
34 #endif
35
36 #ifdef __NR_pwrite64
37 # ifdef __NR_pwrite
38 #  error "__NR_pwrite and __NR_pwrite64 both defined???"
39 # endif
40 # define __NR_pwrite __NR_pwrite64
41 #endif
42
43 /* For Linux we can use the system call table in the header file
44         /usr/include/asm/unistd.h
45    of the kernel.  But these symbols do not follow the SYS_* syntax
46    so we have to redefine the `SYS_ify' macro here.  */
47 #undef SYS_ify
48 #ifdef __STDC__
49 # define SYS_ify(syscall_name)  __NR_##syscall_name
50 #else
51 # define SYS_ify(syscall_name)  __NR_/**/syscall_name
52 #endif
53
54 #ifndef __ASSEMBLER__
55
56 # include <errno.h>
57
58 # ifdef SHARED
59 #  define INLINE_VSYSCALL(name, nr, args...) \
60   ({                                                                          \
61     __label__ out;                                                            \
62     __label__ iserr;                                                          \
63     INTERNAL_SYSCALL_DECL (sc_err);                                           \
64     long int sc_ret;                                                          \
65                                                                               \
66     if (__vdso_##name != NULL)                                                \
67       {                                                                       \
68         sc_ret = INTERNAL_VSYSCALL_NCS (__vdso_##name, sc_err, nr, ##args);   \
69         if (!INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err))                       \
70           goto out;                                                           \
71         if (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err) != ENOSYS)                \
72           goto iserr;                                                         \
73       }                                                                       \
74                                                                               \
75     sc_ret = INTERNAL_SYSCALL (name, sc_err, nr, ##args);                     \
76     if (INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err))                            \
77       {                                                                       \
78       iserr:                                                                  \
79         __set_errno (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err));                \
80         sc_ret = -1L;                                                         \
81       }                                                                       \
82   out:                                                                        \
83     sc_ret;                                                                   \
84   })
85 # else
86 #  define INLINE_VSYSCALL(name, nr, args...) \
87   INLINE_SYSCALL (name, nr, ##args)
88 # endif
89
90 # ifdef SHARED
91 #  define INTERNAL_VSYSCALL(name, err, nr, args...) \
92   ({                                                                          \
93     __label__ out;                                                            \
94     long int v_ret;                                                           \
95                                                                               \
96     if (__vdso_##name != NULL)                                                \
97       {                                                                       \
98         v_ret = INTERNAL_VSYSCALL_NCS (__vdso_##name, err, nr, ##args);       \
99         if (!INTERNAL_SYSCALL_ERROR_P (v_ret, err)                            \
100             || INTERNAL_SYSCALL_ERRNO (v_ret, err) != ENOSYS)                 \
101           goto out;                                                           \
102       }                                                                       \
103     v_ret = INTERNAL_SYSCALL (name, err, nr, ##args);                         \
104   out:                                                                        \
105     v_ret;                                                                    \
106   })
107 # else
108 #  define INTERNAL_VSYSCALL(name, err, nr, args...) \
109   INTERNAL_SYSCALL (name, err, nr, ##args)
110 # endif
111
112 # define INTERNAL_VSYSCALL_NO_SYSCALL_FALLBACK(name, err, nr, args...)        \
113   ({                                                                          \
114     long int sc_ret = ENOSYS;                                                 \
115                                                                               \
116     if (__vdso_##name != NULL)                                                \
117       sc_ret = INTERNAL_VSYSCALL_NCS (__vdso_##name, err, nr, ##args);        \
118     else                                                                      \
119       err = 1 << 28;                                                          \
120     sc_ret;                                                                   \
121   })
122
123 /* List of system calls which are supported as vsyscalls.  */
124 # define HAVE_CLOCK_GETRES_VSYSCALL     1
125 # define HAVE_CLOCK_GETTIME_VSYSCALL    1
126
127 /* Define a macro which expands inline into the wrapper code for a VDSO
128    call. This use is for internal calls that do not need to handle errors
129    normally. It will never touch errno.
130    On powerpc a system call basically clobbers the same registers like a
131    function call, with the exception of LR (which is needed for the
132    "sc; bnslr+" sequence) and CR (where only CR0.SO is clobbered to signal
133    an error return status).  */
134 # define INTERNAL_VSYSCALL_NCS(funcptr, err, nr, args...) \
135   ({                                                                          \
136     register void *r0  __asm__ ("r0");                                        \
137     register long int r3  __asm__ ("r3");                                     \
138     register long int r4  __asm__ ("r4");                                     \
139     register long int r5  __asm__ ("r5");                                     \
140     register long int r6  __asm__ ("r6");                                     \
141     register long int r7  __asm__ ("r7");                                     \
142     register long int r8  __asm__ ("r8");                                     \
143     register long int r9  __asm__ ("r9");                                     \
144     register long int r10 __asm__ ("r10");                                    \
145     register long int r11 __asm__ ("r11");                                    \
146     register long int r12 __asm__ ("r12");                                    \
147     LOADARGS_##nr (funcptr, args);                                            \
148     __asm__ __volatile__                                                      \
149       ("mtctr %0\n\t"                                                         \
150        "bctrl\n\t"                                                            \
151        "mfcr %0"                                                              \
152        : "=&r" (r0),                                                          \
153          "=&r" (r3), "=&r" (r4), "=&r" (r5),  "=&r" (r6),  "=&r" (r7),        \
154          "=&r" (r8), "=&r" (r9), "=&r" (r10), "=&r" (r11), "=&r" (r12)        \
155        : ASM_INPUT_##nr                                                       \
156        : "cr0", "ctr", "lr", "memory");                                       \
157     err = (long int) r0;                                                      \
158     (int) r3;                                                                 \
159   })
160
161 # undef INLINE_SYSCALL
162 # define INLINE_SYSCALL(name, nr, args...)                              \
163   ({                                                                    \
164     INTERNAL_SYSCALL_DECL (sc_err);                                     \
165     long int sc_ret = INTERNAL_SYSCALL (name, sc_err, nr, args);        \
166     if (INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err))                      \
167       {                                                                 \
168         __set_errno (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err));          \
169         sc_ret = -1L;                                                   \
170       }                                                                 \
171     sc_ret;                                                             \
172   })
173
174 /* Define a macro which expands inline into the wrapper code for a system
175    call. This use is for internal calls that do not need to handle errors
176    normally. It will never touch errno.
177    On powerpc a system call basically clobbers the same registers like a
178    function call, with the exception of LR (which is needed for the
179    "sc; bnslr+" sequence) and CR (where only CR0.SO is clobbered to signal
180    an error return status).  */
181
182 # undef INTERNAL_SYSCALL_DECL
183 # define INTERNAL_SYSCALL_DECL(err) long int err
184
185 # undef INTERNAL_SYSCALL
186 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...)                   \
187   ({                                                                    \
188     register long int r0  __asm__ ("r0");                               \
189     register long int r3  __asm__ ("r3");                               \
190     register long int r4  __asm__ ("r4");                               \
191     register long int r5  __asm__ ("r5");                               \
192     register long int r6  __asm__ ("r6");                               \
193     register long int r7  __asm__ ("r7");                               \
194     register long int r8  __asm__ ("r8");                               \
195     register long int r9  __asm__ ("r9");                               \
196     register long int r10 __asm__ ("r10");                              \
197     register long int r11 __asm__ ("r11");                              \
198     register long int r12 __asm__ ("r12");                              \
199     LOADARGS_##nr(name, args);                                  \
200     __asm__ __volatile__                                                \
201       ("sc   \n\t"                                                      \
202        "mfcr %0"                                                        \
203        : "=&r" (r0),                                                    \
204          "=&r" (r3), "=&r" (r4), "=&r" (r5),  "=&r" (r6),  "=&r" (r7),  \
205          "=&r" (r8), "=&r" (r9), "=&r" (r10), "=&r" (r11), "=&r" (r12)  \
206        : ASM_INPUT_##nr                                                 \
207        : "cr0", "ctr", "memory");                                       \
208     err = r0;                                                           \
209     (int) r3;                                                           \
210   })
211 # define INTERNAL_SYSCALL(name, err, nr, args...) \
212   INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
213
214 # undef INTERNAL_SYSCALL_ERROR_P
215 # define INTERNAL_SYSCALL_ERROR_P(val, err) \
216   ((void) (val), __builtin_expect ((err) & (1 << 28), 0))
217
218 # undef INTERNAL_SYSCALL_ERRNO
219 # define INTERNAL_SYSCALL_ERRNO(val, err)     (val)
220
221 # define LOADARGS_0(name, dummy)                                              \
222         r0 = name
223 # define LOADARGS_1(name, __arg1) \
224         long int arg1 = (long int) (__arg1);    \
225   LOADARGS_0(name, 0);                                     \
226         extern void __illegally_sized_syscall_arg1 (void); \
227         if (__builtin_classify_type (__arg1) != 5 && sizeof (__arg1) > 4) \
228           __illegally_sized_syscall_arg1 (); \
229         r3 = arg1
230 # define LOADARGS_2(name, __arg1, __arg2) \
231         long int arg2 = (long int) (__arg2); \
232         LOADARGS_1(name, __arg1); \
233         extern void __illegally_sized_syscall_arg2 (void); \
234         if (__builtin_classify_type (__arg2) != 5 && sizeof (__arg2) > 4) \
235           __illegally_sized_syscall_arg2 (); \
236         r4 = arg2
237 # define LOADARGS_3(name, __arg1, __arg2, __arg3) \
238         long int arg3 = (long int) (__arg3); \
239         LOADARGS_2(name, __arg1, __arg2); \
240         extern void __illegally_sized_syscall_arg3 (void); \
241         if (__builtin_classify_type (__arg3) != 5 && sizeof (__arg3) > 4) \
242           __illegally_sized_syscall_arg3 (); \
243         r5 = arg3
244 # define LOADARGS_4(name, __arg1, __arg2, __arg3, __arg4) \
245         long int arg4 = (long int) (__arg4); \
246         LOADARGS_3(name, __arg1, __arg2, __arg3); \
247         extern void __illegally_sized_syscall_arg4 (void); \
248         if (__builtin_classify_type (__arg4) != 5 && sizeof (__arg4) > 4) \
249           __illegally_sized_syscall_arg4 (); \
250         r6 = arg4
251 # define LOADARGS_5(name, __arg1, __arg2, __arg3, __arg4, __arg5) \
252         long int arg5 = (long int) (__arg5); \
253         LOADARGS_4(name, __arg1, __arg2, __arg3, __arg4); \
254         extern void __illegally_sized_syscall_arg5 (void); \
255         if (__builtin_classify_type (__arg5) != 5 && sizeof (__arg5) > 4) \
256           __illegally_sized_syscall_arg5 (); \
257         r7 = arg5
258 # define LOADARGS_6(name, __arg1, __arg2, __arg3, __arg4, __arg5, __arg6) \
259         long int arg6 = (long int) (__arg6); \
260         LOADARGS_5(name, __arg1, __arg2, __arg3, __arg4, __arg5); \
261         extern void __illegally_sized_syscall_arg6 (void); \
262         if (__builtin_classify_type (__arg6) != 5 && sizeof (__arg6) > 4) \
263           __illegally_sized_syscall_arg6 (); \
264         r8 = arg6
265
266 # define ASM_INPUT_0 "0" (r0)
267 # define ASM_INPUT_1 ASM_INPUT_0, "1" (r3)
268 # define ASM_INPUT_2 ASM_INPUT_1, "2" (r4)
269 # define ASM_INPUT_3 ASM_INPUT_2, "3" (r5)
270 # define ASM_INPUT_4 ASM_INPUT_3, "4" (r6)
271 # define ASM_INPUT_5 ASM_INPUT_4, "5" (r7)
272 # define ASM_INPUT_6 ASM_INPUT_5, "6" (r8)
273
274 #endif /* __ASSEMBLER__ */
275
276
277 /* Pointer mangling support.  */
278 #if defined NOT_IN_libc && defined IS_IN_rtld
279 /* We cannot use the thread descriptor because in ld.so we use setjmp
280    earlier than the descriptor is initialized.  */
281 #else
282 # ifdef __ASSEMBLER__
283 #  define PTR_MANGLE(reg, tmpreg) \
284         lwz     tmpreg,POINTER_GUARD(r2); \
285         xor     reg,tmpreg,reg
286 #  define PTR_MANGLE2(reg, tmpreg) \
287         xor     reg,tmpreg,reg
288 #  define PTR_MANGLE3(destreg, reg, tmpreg) \
289         lwz     tmpreg,POINTER_GUARD(r2); \
290         xor     destreg,tmpreg,reg
291 #  define PTR_DEMANGLE(reg, tmpreg) PTR_MANGLE (reg, tmpreg)
292 #  define PTR_DEMANGLE2(reg, tmpreg) PTR_MANGLE2 (reg, tmpreg)
293 #  define PTR_DEMANGLE3(destreg, reg, tmpreg) PTR_MANGLE3 (destreg, reg, tmpreg)
294 # else
295 #  define PTR_MANGLE(var) \
296   (var) = (__typeof (var)) ((uintptr_t) (var) ^ THREAD_GET_POINTER_GUARD ())
297 #  define PTR_DEMANGLE(var)     PTR_MANGLE (var)
298 # endif
299 #endif
300
301 #endif /* linux/powerpc/powerpc32/sysdep.h */