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