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>
31 /* In order to get __set_errno() definition in INLINE_SYSCALL. */
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. */
41 #define SYS_ify(syscall_name) (__NR_##syscall_name)
44 #include <bits/hwcap.h>
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. */
52 # define GET_TLS_BODY \
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. */
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. */
67 #define GET_TLS(TMP) \
70 cfi_register (lr, TMP); \
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
90 #define PSEUDO(name, syscall_name, args) \
93 DO_CALL (syscall_name, args); \
99 b PLTJMP(SYSCALL_ERROR)
101 #define ret PSEUDO_RET
104 #define PSEUDO_END(name) \
105 SYSCALL_ERROR_HANDLER; \
108 #undef PSEUDO_NOERRNO
109 #define PSEUDO_NOERRNO(name, syscall_name, args) \
112 DO_CALL (syscall_name, args);
114 #define PSEUDO_RET_NOERRNO \
118 #define ret_NOERRNO PSEUDO_RET_NOERRNO
120 #undef PSEUDO_END_NOERRNO
121 #define PSEUDO_END_NOERRNO(name) \
124 /* The function has to return the error code. */
126 #define PSEUDO_ERRVAL(name, syscall_name, args) \
129 DO_CALL (syscall_name, args); \
132 #undef PSEUDO_END_ERRVAL
133 #define PSEUDO_END_ERRVAL(name) \
136 #define ret_ERRVAL PSEUDO_RET_NOERRNO
139 # define SYSCALL_ERROR __local_syscall_error
140 # if RTLD_PRIVATE_ERRNO
141 # define SYSCALL_ERROR_HANDLER \
142 __local_syscall_error: \
144 LDST_PCREL(str, r0, r1, C_SYMBOL_NAME(rtld_errno)); \
148 # if defined(__ARM_ARCH_4T__) && defined(__THUMB_INTERWORK__)
151 cfi_adjust_cfa_offset (-4); \
155 # define POP_PC pop { pc }
157 # define SYSCALL_ERROR_HANDLER \
158 __local_syscall_error: \
160 cfi_adjust_cfa_offset (4); \
161 cfi_rel_offset (lr, 0); \
163 cfi_adjust_cfa_offset (4); \
164 bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
166 cfi_adjust_cfa_offset (-4); \
173 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
174 # define SYSCALL_ERROR __syscall_error
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
184 /* Linux takes system call args in registers:
189 arg 5 r4 (this is different from the APCS convention)
193 The compiler is going to form a call by coming here, through PSEUDO, with
195 syscall number in the DO_CALL macro
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
223 #define DO_CALL(syscall_name, args) \
225 ldr r7, =SYS_ify (syscall_name); \
233 cfi_adjust_cfa_offset (4); \
234 cfi_rel_offset (r7, 0); \
237 #define DOARGS_1 DOARGS_0
239 #define DOARGS_2 DOARGS_0
241 #define DOARGS_3 DOARGS_0
243 #define DOARGS_4 DOARGS_0
248 cfi_adjust_cfa_offset (8); \
249 cfi_rel_offset (r4, 0); \
250 cfi_rel_offset (r7, 4); \
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 }; \
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}
280 cfi_adjust_cfa_offset (-4); \
284 #define UNDOARGS_1 UNDOARGS_0
286 #define UNDOARGS_2 UNDOARGS_0
288 #define UNDOARGS_3 UNDOARGS_0
290 #define UNDOARGS_4 UNDOARGS_0
294 cfi_adjust_cfa_offset (-8); \
301 cfi_adjust_cfa_offset (-12); \
308 pop {r4, r5, r6, r7}; \
309 cfi_adjust_cfa_offset (-16); \
316 #else /* not __ASSEMBLER__ */
318 /* Define a macro which expands into the inline wrapper code for a system
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
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...) \
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" \
357 : "r" (_name) ASM_ARGS_##nr \
361 # undef INTERNAL_SYSCALL_RAW
362 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
364 register int _a1 asm ("r0"), _nr asm ("r7"); \
365 LOAD_ARGS_##nr (args) \
367 asm volatile ("swi 0x0 @ syscall " #name \
369 : "r" (_nr) ASM_ARGS_##nr \
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()
391 #define LOAD_ARGS_1(a1) \
392 int _a1tmp = (int) (a1); \
395 #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
396 #define LOAD_ARGS_2(a1, a2) \
397 int _a2tmp = (int) (a2); \
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)
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)
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 */