arm: Use push/pop mnemonics
[glibc.git] / ports / sysdeps / unix / sysv / linux / arm / sysdep.h
blobf2ab5f589f06238aa379ae721685e5ac2ed362e0
1 /* Copyright (C) 1992-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>, August 1995.
4 ARM changes by Philip Blundell, <pjb27@cam.ac.uk>, May 1997.
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, see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef _LINUX_ARM_SYSDEP_H
21 #define _LINUX_ARM_SYSDEP_H 1
23 /* There is some commonality. */
24 #include <sysdeps/unix/arm/sysdep.h>
26 /* Defines RTLD_PRIVATE_ERRNO and USE_DL_SYSINFO. */
27 #include <dl-sysdep.h>
29 #include <tls.h>
31 /* In order to get __set_errno() definition in INLINE_SYSCALL. */
32 #ifndef __ASSEMBLER__
33 #include <errno.h>
34 #endif
36 /* For Linux we can use the system call table in the header file
37 /usr/include/asm/unistd.h
38 of the kernel. But these symbols do not follow the SYS_* syntax
39 so we have to redefine the `SYS_ify' macro here. */
40 #undef SYS_ify
41 #define SYS_ify(syscall_name) (__NR_##syscall_name)
43 #define _SYS_AUXV_H 1
44 #include <bits/hwcap.h>
46 #ifdef __ASSEMBLER__
48 /* Internal macro calling the linux kernel kuser_get_tls helper.
49 Note that in thumb mode, a constant pool break is often out of range, so
50 we always expand the constant inline. */
51 #ifdef __thumb2__
52 # define GET_TLS_BODY \
53 movw r0, #0x0fe0; \
54 movt r0, #0xffff; \
55 blx r0
56 #else
57 # define GET_TLS_BODY \
58 mov r0, #0xffff0fff; /* Point to the high page. */ \
59 mov lr, pc; /* Save our return address. */ \
60 sub pc, r0, #31 /* Jump to the TLS entry. */
61 #endif
63 /* Helper to get the TLS base pointer. Save LR in TMP, return in R0,
64 and no other registers clobbered. TMP may be LR itself to indicate
65 that no save is necessary. */
66 #undef GET_TLS
67 #define GET_TLS(TMP) \
68 .ifnc TMP, lr; \
69 mov TMP, lr; \
70 cfi_register (lr, TMP); \
71 GET_TLS_BODY; \
72 mov lr, TMP; \
73 cfi_restore (lr); \
74 .else; \
75 GET_TLS_BODY; \
76 .endif
78 /* Linux uses a negative return value to indicate syscall errors,
79 unlike most Unices, which use the condition codes' carry flag.
81 Since version 2.1 the return value of a system call might be
82 negative even if the call succeeded. E.g., the `lseek' system call
83 might return a large offset. Therefore we must not anymore test
84 for < 0, but test for a real error by making sure the value in R0
85 is a real error number. Linus said he will make sure the no syscall
86 returns a value in -1 .. -4095 as a valid result so we can safely
87 test with -4095. */
89 #undef PSEUDO
90 #define PSEUDO(name, syscall_name, args) \
91 .text; \
92 ENTRY (name); \
93 DO_CALL (syscall_name, args); \
94 cmn r0, $4096;
96 #define PSEUDO_RET \
97 it cc; \
98 RETINSTR(cc, lr); \
99 b PLTJMP(SYSCALL_ERROR)
100 #undef ret
101 #define ret PSEUDO_RET
103 #undef PSEUDO_END
104 #define PSEUDO_END(name) \
105 SYSCALL_ERROR_HANDLER; \
106 END (name)
108 #undef PSEUDO_NOERRNO
109 #define PSEUDO_NOERRNO(name, syscall_name, args) \
110 .text; \
111 ENTRY (name); \
112 DO_CALL (syscall_name, args);
114 #define PSEUDO_RET_NOERRNO \
115 DO_RET (lr);
117 #undef ret_NOERRNO
118 #define ret_NOERRNO PSEUDO_RET_NOERRNO
120 #undef PSEUDO_END_NOERRNO
121 #define PSEUDO_END_NOERRNO(name) \
122 END (name)
124 /* The function has to return the error code. */
125 #undef PSEUDO_ERRVAL
126 #define PSEUDO_ERRVAL(name, syscall_name, args) \
127 .text; \
128 ENTRY (name) \
129 DO_CALL (syscall_name, args); \
130 rsb r0, r0, #0
132 #undef PSEUDO_END_ERRVAL
133 #define PSEUDO_END_ERRVAL(name) \
134 END (name)
136 #define ret_ERRVAL PSEUDO_RET_NOERRNO
138 #if NOT_IN_libc
139 # define SYSCALL_ERROR __local_syscall_error
140 # if RTLD_PRIVATE_ERRNO
141 # define SYSCALL_ERROR_HANDLER \
142 __local_syscall_error: \
143 rsb r0, r0, #0; \
144 LDST_PCREL(str, r0, r1, C_SYMBOL_NAME(rtld_errno)); \
145 mvn r0, #0; \
146 DO_RET(lr)
147 # else
148 # if defined(__ARM_ARCH_4T__) && defined(__THUMB_INTERWORK__)
149 # define POP_PC \
150 pop { lr }; \
151 cfi_adjust_cfa_offset (-4); \
152 cfi_restore (lr); \
153 bx lr
154 # else
155 # define POP_PC pop { pc }
156 # endif
157 # define SYSCALL_ERROR_HANDLER \
158 __local_syscall_error: \
159 push { lr }; \
160 cfi_adjust_cfa_offset (4); \
161 cfi_rel_offset (lr, 0); \
162 push { r0 }; \
163 cfi_adjust_cfa_offset (4); \
164 bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
165 pop { r1 }; \
166 cfi_adjust_cfa_offset (-4); \
167 rsb r1, r1, #0; \
168 str r1, [r0]; \
169 mvn r0, #0; \
170 POP_PC;
171 # endif
172 #else
173 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
174 # define SYSCALL_ERROR __syscall_error
175 #endif
177 /* The ARM EABI user interface passes the syscall number in r7, instead
178 of in the swi. This is more efficient, because the kernel does not need
179 to fetch the swi from memory to find out the number; which can be painful
180 with separate I-cache and D-cache. Make sure to use 0 for the SWI
181 argument; otherwise the (optional) compatibility code for APCS binaries
182 may be invoked. */
184 /* Linux takes system call args in registers:
185 arg 1 r0
186 arg 2 r1
187 arg 3 r2
188 arg 4 r3
189 arg 5 r4 (this is different from the APCS convention)
190 arg 6 r5
191 arg 7 r6
193 The compiler is going to form a call by coming here, through PSEUDO, with
194 arguments
195 syscall number in the DO_CALL macro
196 arg 1 r0
197 arg 2 r1
198 arg 3 r2
199 arg 4 r3
200 arg 5 [sp]
201 arg 6 [sp+4]
202 arg 7 [sp+8]
204 We need to shuffle values between R4..R6 and the stack so that the
205 caller's v1..v3 and stack frame are not corrupted, and the kernel
206 sees the right arguments.
210 /* We must save and restore r7 (call-saved) for the syscall number.
211 We never make function calls from inside here (only potentially
212 signal handlers), so we do not bother with doubleword alignment.
214 Just like the APCS syscall convention, the EABI syscall convention uses
215 r0 through r6 for up to seven syscall arguments. None are ever passed to
216 the kernel on the stack, although incoming arguments are on the stack for
217 syscalls with five or more arguments.
219 The assembler will convert the literal pool load to a move for most
220 syscalls. */
222 #undef DO_CALL
223 #define DO_CALL(syscall_name, args) \
224 DOARGS_##args; \
225 ldr r7, =SYS_ify (syscall_name); \
226 swi 0x0; \
227 UNDOARGS_##args
229 #undef DOARGS_0
230 #define DOARGS_0 \
231 .fnstart; \
232 push { r7 }; \
233 cfi_adjust_cfa_offset (4); \
234 cfi_rel_offset (r7, 0); \
235 .save { r7 }
236 #undef DOARGS_1
237 #define DOARGS_1 DOARGS_0
238 #undef DOARGS_2
239 #define DOARGS_2 DOARGS_0
240 #undef DOARGS_3
241 #define DOARGS_3 DOARGS_0
242 #undef DOARGS_4
243 #define DOARGS_4 DOARGS_0
244 #undef DOARGS_5
245 #define DOARGS_5 \
246 .fnstart; \
247 push {r4, r7}; \
248 cfi_adjust_cfa_offset (8); \
249 cfi_rel_offset (r4, 0); \
250 cfi_rel_offset (r7, 4); \
251 .save { r4, r7 }; \
252 ldr r4, [sp, #8]
253 #undef DOARGS_6
254 #define DOARGS_6 \
255 .fnstart; \
256 mov ip, sp; \
257 push {r4, r5, r7}; \
258 cfi_adjust_cfa_offset (12); \
259 cfi_rel_offset (r4, 0); \
260 cfi_rel_offset (r5, 4); \
261 cfi_rel_offset (r7, 8); \
262 .save { r4, r5, r7 }; \
263 ldmia ip, {r4, r5}
264 #undef DOARGS_7
265 #define DOARGS_7 \
266 .fnstart; \
267 mov ip, sp; \
268 push {r4, r5, r6, r7}; \
269 cfi_adjust_cfa_offset (16); \
270 cfi_rel_offset (r4, 0); \
271 cfi_rel_offset (r5, 4); \
272 cfi_rel_offset (r6, 8); \
273 cfi_rel_offset (r7, 12); \
274 .save { r4, r5, r6, r7 }; \
275 ldmia ip, {r4, r5, r6}
277 #undef UNDOARGS_0
278 #define UNDOARGS_0 \
279 pop {r7}; \
280 cfi_adjust_cfa_offset (-4); \
281 cfi_restore (r7); \
282 .fnend
283 #undef UNDOARGS_1
284 #define UNDOARGS_1 UNDOARGS_0
285 #undef UNDOARGS_2
286 #define UNDOARGS_2 UNDOARGS_0
287 #undef UNDOARGS_3
288 #define UNDOARGS_3 UNDOARGS_0
289 #undef UNDOARGS_4
290 #define UNDOARGS_4 UNDOARGS_0
291 #undef UNDOARGS_5
292 #define UNDOARGS_5 \
293 pop {r4, r7}; \
294 cfi_adjust_cfa_offset (-8); \
295 cfi_restore (r4); \
296 cfi_restore (r7); \
297 .fnend
298 #undef UNDOARGS_6
299 #define UNDOARGS_6 \
300 pop {r4, r5, r7}; \
301 cfi_adjust_cfa_offset (-12); \
302 cfi_restore (r4); \
303 cfi_restore (r5); \
304 cfi_restore (r7); \
305 .fnend
306 #undef UNDOARGS_7
307 #define UNDOARGS_7 \
308 pop {r4, r5, r6, r7}; \
309 cfi_adjust_cfa_offset (-16); \
310 cfi_restore (r4); \
311 cfi_restore (r5); \
312 cfi_restore (r6); \
313 cfi_restore (r7); \
314 .fnend
316 #else /* not __ASSEMBLER__ */
318 /* Define a macro which expands into the inline wrapper code for a system
319 call. */
320 #undef INLINE_SYSCALL
321 #define INLINE_SYSCALL(name, nr, args...) \
322 ({ unsigned int _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
323 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0)) \
325 __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
326 _sys_result = (unsigned int) -1; \
328 (int) _sys_result; })
330 #undef INTERNAL_SYSCALL_DECL
331 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
333 #if defined(__thumb__)
334 /* We can not expose the use of r7 to the compiler. GCC (as
335 of 4.5) uses r7 as the hard frame pointer for Thumb - although
336 for Thumb-2 it isn't obviously a better choice than r11.
337 And GCC does not support asms that conflict with the frame
338 pointer.
340 This would be easier if syscall numbers never exceeded 255,
341 but they do. For the moment the LOAD_ARGS_7 is sacrificed.
342 We can't use push/pop inside the asm because that breaks
343 unwinding (i.e. thread cancellation) for this frame. We can't
344 locally save and restore r7, because we do not know if this
345 function uses r7 or if it is our caller's r7; if it is our caller's,
346 then unwinding will fail higher up the stack. So we move the
347 syscall out of line and provide its own unwind information. */
348 # undef INTERNAL_SYSCALL_RAW
349 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
350 ({ \
351 register int _a1 asm ("a1"); \
352 int _nametmp = name; \
353 LOAD_ARGS_##nr (args) \
354 register int _name asm ("ip") = _nametmp; \
355 asm volatile ("bl __libc_do_syscall" \
356 : "=r" (_a1) \
357 : "r" (_name) ASM_ARGS_##nr \
358 : "memory", "lr"); \
359 _a1; })
360 #else /* ARM */
361 # undef INTERNAL_SYSCALL_RAW
362 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
363 ({ \
364 register int _a1 asm ("r0"), _nr asm ("r7"); \
365 LOAD_ARGS_##nr (args) \
366 _nr = name; \
367 asm volatile ("swi 0x0 @ syscall " #name \
368 : "=r" (_a1) \
369 : "r" (_nr) ASM_ARGS_##nr \
370 : "memory"); \
371 _a1; })
372 #endif
374 #undef INTERNAL_SYSCALL
375 #define INTERNAL_SYSCALL(name, err, nr, args...) \
376 INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
378 #undef INTERNAL_SYSCALL_ARM
379 #define INTERNAL_SYSCALL_ARM(name, err, nr, args...) \
380 INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
382 #undef INTERNAL_SYSCALL_ERROR_P
383 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
384 ((unsigned int) (val) >= 0xfffff001u)
386 #undef INTERNAL_SYSCALL_ERRNO
387 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
389 #define LOAD_ARGS_0()
390 #define ASM_ARGS_0
391 #define LOAD_ARGS_1(a1) \
392 int _a1tmp = (int) (a1); \
393 LOAD_ARGS_0 () \
394 _a1 = _a1tmp;
395 #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
396 #define LOAD_ARGS_2(a1, a2) \
397 int _a2tmp = (int) (a2); \
398 LOAD_ARGS_1 (a1) \
399 register int _a2 asm ("a2") = _a2tmp;
400 #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
401 #define LOAD_ARGS_3(a1, a2, a3) \
402 int _a3tmp = (int) (a3); \
403 LOAD_ARGS_2 (a1, a2) \
404 register int _a3 asm ("a3") = _a3tmp;
405 #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
406 #define LOAD_ARGS_4(a1, a2, a3, a4) \
407 int _a4tmp = (int) (a4); \
408 LOAD_ARGS_3 (a1, a2, a3) \
409 register int _a4 asm ("a4") = _a4tmp;
410 #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
411 #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
412 int _v1tmp = (int) (a5); \
413 LOAD_ARGS_4 (a1, a2, a3, a4) \
414 register int _v1 asm ("v1") = _v1tmp;
415 #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
416 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
417 int _v2tmp = (int) (a6); \
418 LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
419 register int _v2 asm ("v2") = _v2tmp;
420 #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
421 #ifndef __thumb__
422 # define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
423 int _v3tmp = (int) (a7); \
424 LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6) \
425 register int _v3 asm ("v3") = _v3tmp;
426 # define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
427 #endif
429 /* For EABI, non-constant syscalls are actually pretty easy... */
430 #undef INTERNAL_SYSCALL_NCS
431 #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
432 INTERNAL_SYSCALL_RAW (number, err, nr, args)
434 #endif /* __ASSEMBLER__ */
436 /* Pointer mangling is not yet supported for ARM. */
437 #define PTR_MANGLE(var) (void) (var)
438 #define PTR_DEMANGLE(var) (void) (var)
440 #endif /* linux/arm/sysdep.h */