2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / s390 / s390-32 / sysdep.h
blobb86072bd30577cebd5491f3239fc372e25fdbe28
1 /* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
2 Free Software Foundation, Inc.
3 Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
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
19 02111-1307 USA. */
21 #ifndef _LINUX_S390_SYSDEP_H
22 #define _LINUX_S390_SYSDEP_H
24 #include <sysdeps/s390/s390-32/sysdep.h>
25 #include <sysdeps/unix/sysdep.h>
26 #include <dl-sysdep.h> /* For RTLD_PRIVATE_ERRNO. */
27 #include <tls.h>
29 /* For Linux we can use the system call table in the header file
30 /usr/include/asm/unistd.h
31 of the kernel. But these symbols do not follow the SYS_* syntax
32 so we have to redefine the `SYS_ify' macro here. */
33 /* in newer 2.1 kernels __NR_syscall is missing so we define it here */
34 #define __NR_syscall 0
36 #undef SYS_ify
37 #define SYS_ify(syscall_name) __NR_##syscall_name
39 #ifdef __ASSEMBLER__
41 /* Linux uses a negative return value to indicate syscall errors, unlike
42 most Unices, which use the condition codes' carry flag.
44 Since version 2.1 the return value of a system call might be negative
45 even if the call succeeded. E.g., the `lseek' system call might return
46 a large offset. Therefore we must not anymore test for < 0, but test
47 for a real error by making sure the value in gpr2 is a real error
48 number. Linus said he will make sure the no syscall returns a value
49 in -1 .. -4095 as a valid result so we can savely test with -4095. */
51 #undef PSEUDO
52 #define PSEUDO(name, syscall_name, args) \
53 .text; \
54 ENTRY (name) \
55 DO_CALL (syscall_name, args); \
56 lhi %r4,-4095 ; \
57 clr %r2,%r4 ; \
58 jnl SYSCALL_ERROR_LABEL
60 #undef PSEUDO_END
61 #define PSEUDO_END(name) \
62 SYSCALL_ERROR_HANDLER; \
63 END (name)
65 #undef PSEUDO_NOERRNO
66 #define PSEUDO_NOERRNO(name, syscall_name, args) \
67 .text; \
68 ENTRY (name) \
69 DO_CALL (syscall_name, args)
71 #undef PSEUDO_END_NOERRNO
72 #define PSEUDO_END_NOERRNO(name) \
73 END (name)
75 #undef PSEUDO_ERRVAL
76 #define PSEUDO_ERRVAL(name, syscall_name, args) \
77 .text; \
78 ENTRY (name) \
79 DO_CALL (syscall_name, args); \
80 lcr %r2,%r2
82 #undef PSEUDO_END_ERRVAL
83 #define PSEUDO_END_ERRVAL(name) \
84 END (name)
86 #ifndef PIC
87 # define SYSCALL_ERROR_LABEL 0f
88 # define SYSCALL_ERROR_HANDLER \
89 0: basr %r1,0; \
90 1: l %r1,2f-1b(%r1); \
91 br %r1; \
92 2: .long syscall_error
93 #else
94 # if RTLD_PRIVATE_ERRNO
95 # define SYSCALL_ERROR_LABEL 0f
96 # define SYSCALL_ERROR_HANDLER \
97 0: basr %r1,0; \
98 1: al %r1,2f-1b(%r1); \
99 lcr %r2,%r2; \
100 st %r2,0(%r1); \
101 lhi %r2,-1; \
102 br %r14; \
103 2: .long rtld_errno-1b
104 # elif defined _LIBC_REENTRANT
105 # if USE___THREAD
106 # ifndef NOT_IN_libc
107 # define SYSCALL_ERROR_ERRNO __libc_errno
108 # else
109 # define SYSCALL_ERROR_ERRNO errno
110 # endif
111 # define SYSCALL_ERROR_LABEL 0f
112 # define SYSCALL_ERROR_HANDLER \
113 0: lcr %r0,%r2; \
114 basr %r1,0; \
115 1: al %r1,2f-1b(%r1); \
116 l %r1,SYSCALL_ERROR_ERRNO@gotntpoff(%r1); \
117 ear %r2,%a0; \
118 st %r0,0(%r1,%r2); \
119 lhi %r2,-1; \
120 br %r14; \
121 2: .long _GLOBAL_OFFSET_TABLE_-1b
122 # else
123 # define SYSCALL_ERROR_LABEL 0f
124 # define SYSCALL_ERROR_HANDLER \
125 0: basr %r1,0; \
126 1: al %r1,2f-1b(%r1); \
127 br %r1; \
128 2: .long syscall_error@plt-1b
129 # endif
130 # else
131 # define SYSCALL_ERROR_LABEL 0f
132 # define SYSCALL_ERROR_HANDLER \
133 0: basr %r1,0; \
134 1: al %r1,2f-1b(%r1); \
135 l %r1,errno@GOT(%r1); \
136 lcr %r2,%r2; \
137 st %r2,0(%r1); \
138 lhi %r2,-1; \
139 br %r14; \
140 2: .long _GLOBAL_OFFSET_TABLE_-1b
141 # endif /* _LIBC_REENTRANT */
142 #endif /* PIC */
144 /* Linux takes system call arguments in registers:
146 syscall number 1 call-clobbered
147 arg 1 2 call-clobbered
148 arg 2 3 call-clobbered
149 arg 3 4 call-clobbered
150 arg 4 5 call-clobbered
151 arg 5 6 call-saved
152 arg 6 7 call-saved
154 (Of course a function with say 3 arguments does not have entries for
155 arguments 4 and 5.)
156 For system calls with 6 parameters a stack operation is required
157 to load the 6th parameter to register 7. Call saved register 7 is
158 moved to register 0 and back to avoid an additional stack frame.
161 #define DO_CALL(syscall, args) \
162 .if args > 5; \
163 lr %r0,%r7; \
164 l %r7,96(%r15); \
165 .endif; \
166 .if SYS_ify (syscall) < 256; \
167 svc SYS_ify (syscall); \
168 .else; \
169 lhi %r1,SYS_ify (syscall); \
170 svc 0; \
171 .endif; \
172 .if args > 5; \
173 lr %r7,%r0; \
174 .endif
176 #define ret \
177 br 14
179 #define ret_NOERRNO \
180 br 14
182 #define ret_ERRVAL \
183 br 14
185 #endif /* __ASSEMBLER__ */
187 #undef INLINE_SYSCALL
188 #define INLINE_SYSCALL(name, nr, args...) \
189 ({ \
190 unsigned int _ret = INTERNAL_SYSCALL (name, , nr, args); \
191 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_ret, ), 0)) \
193 __set_errno (INTERNAL_SYSCALL_ERRNO (_ret, )); \
194 _ret = 0xffffffff; \
196 (int) _ret; })
198 #undef INTERNAL_SYSCALL_DECL
199 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
201 #undef INTERNAL_SYSCALL_DIRECT
202 #define INTERNAL_SYSCALL_DIRECT(name, err, nr, args...) \
203 ({ \
204 DECLARGS_##nr(args) \
205 register int _ret asm("2"); \
206 asm volatile ( \
207 "svc %b1\n\t" \
208 : "=d" (_ret) \
209 : "i" (__NR_##name) ASMFMT_##nr \
210 : "memory" ); \
211 _ret; })
213 #undef INTERNAL_SYSCALL_SVC0
214 #define INTERNAL_SYSCALL_SVC0(name, err, nr, args...) \
215 ({ \
216 DECLARGS_##nr(args) \
217 register unsigned long _nr asm("1") = (unsigned long)(__NR_##name); \
218 register int _ret asm("2"); \
219 asm volatile ( \
220 "svc 0\n\t" \
221 : "=d" (_ret) \
222 : "d" (_nr) ASMFMT_##nr \
223 : "memory" ); \
224 _ret; })
226 #undef INTERNAL_SYSCALL_NCS
227 #define INTERNAL_SYSCALL_NCS(no, err, nr, args...) \
228 ({ \
229 DECLARGS_##nr(args) \
230 register unsigned long _nr asm("1") = (unsigned long)(no); \
231 register int _ret asm("2"); \
232 asm volatile ( \
233 "svc 0\n\t" \
234 : "=d" (_ret) \
235 : "d" (_nr) ASMFMT_##nr \
236 : "memory" ); \
237 _ret; })
239 #undef INTERNAL_SYSCALL
240 #define INTERNAL_SYSCALL(name, err, nr, args...) \
241 (((__NR_##name) < 256) ? \
242 INTERNAL_SYSCALL_DIRECT(name, err, nr, args) : \
243 INTERNAL_SYSCALL_SVC0(name, err,nr, args))
245 #undef INTERNAL_SYSCALL_ERROR_P
246 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
247 ((unsigned int) (val) >= 0xfffff001u)
249 #undef INTERNAL_SYSCALL_ERRNO
250 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
252 #define DECLARGS_0()
253 #define DECLARGS_1(arg1) \
254 register unsigned long gpr2 asm ("2") = (unsigned long)(arg1);
255 #define DECLARGS_2(arg1, arg2) \
256 DECLARGS_1(arg1) \
257 register unsigned long gpr3 asm ("3") = (unsigned long)(arg2);
258 #define DECLARGS_3(arg1, arg2, arg3) \
259 DECLARGS_2(arg1, arg2) \
260 register unsigned long gpr4 asm ("4") = (unsigned long)(arg3);
261 #define DECLARGS_4(arg1, arg2, arg3, arg4) \
262 DECLARGS_3(arg1, arg2, arg3) \
263 register unsigned long gpr5 asm ("5") = (unsigned long)(arg4);
264 #define DECLARGS_5(arg1, arg2, arg3, arg4, arg5) \
265 DECLARGS_4(arg1, arg2, arg3, arg4) \
266 register unsigned long gpr6 asm ("6") = (unsigned long)(arg5);
267 #define DECLARGS_6(arg1, arg2, arg3, arg4, arg5, arg6) \
268 DECLARGS_5(arg1, arg2, arg3, arg4, arg5) \
269 register unsigned long gpr7 asm ("7") = (unsigned long)(arg6);
271 #define ASMFMT_0
272 #define ASMFMT_1 , "0" (gpr2)
273 #define ASMFMT_2 , "0" (gpr2), "d" (gpr3)
274 #define ASMFMT_3 , "0" (gpr2), "d" (gpr3), "d" (gpr4)
275 #define ASMFMT_4 , "0" (gpr2), "d" (gpr3), "d" (gpr4), "d" (gpr5)
276 #define ASMFMT_5 , "0" (gpr2), "d" (gpr3), "d" (gpr4), "d" (gpr5), "d" (gpr6)
277 #define ASMFMT_6 , "0" (gpr2), "d" (gpr3), "d" (gpr4), "d" (gpr5), "d" (gpr6), "d" (gpr7)
280 /* Pointer mangling support. */
281 #if defined NOT_IN_libc && defined IS_IN_rtld
282 /* We cannot use the thread descriptor because in ld.so we use setjmp
283 earlier than the descriptor is initialized. */
284 #else
285 /* For the time being just use stack_guard rather than a separate
286 pointer_guard. */
287 # ifdef __ASSEMBLER__
288 # define PTR_MANGLE(reg, tmpreg) \
289 ear tmpreg,%a0; \
290 x reg,STACK_GUARD(tmpreg)
291 # define PTR_MANGLE2(reg, tmpreg) \
292 x reg,STACK_GUARD(tmpreg)
293 # define PTR_DEMANGLE(reg, tmpreg) PTR_MANGLE (reg, tmpreg)
294 # else
295 # define PTR_MANGLE(var) \
296 (var) = (void *) ((uintptr_t) (var) ^ THREAD_GET_POINTER_GUARD ())
297 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
298 # endif
299 #endif
301 #endif /* _LINUX_S390_SYSDEP_H */