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); \
158 # define SYSCALL_ERROR_HANDLER \
159 __local_syscall_error: \
160 str lr, [sp, #-4]!; \
161 cfi_adjust_cfa_offset (4); \
162 cfi_rel_offset (lr, 0); \
163 str r0, [sp, #-4]!; \
164 cfi_adjust_cfa_offset (4); \
165 bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
167 cfi_adjust_cfa_offset (-4); \
174 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
175 # define SYSCALL_ERROR __syscall_error
178 /* The ARM EABI user interface passes the syscall number in r7, instead
179 of in the swi. This is more efficient, because the kernel does not need
180 to fetch the swi from memory to find out the number; which can be painful
181 with separate I-cache and D-cache. Make sure to use 0 for the SWI
182 argument; otherwise the (optional) compatibility code for APCS binaries
185 /* Linux takes system call args in registers:
190 arg 5 r4 (this is different from the APCS convention)
194 The compiler is going to form a call by coming here, through PSEUDO, with
196 syscall number in the DO_CALL macro
205 We need to shuffle values between R4..R6 and the stack so that the
206 caller's v1..v3 and stack frame are not corrupted, and the kernel
207 sees the right arguments.
211 /* We must save and restore r7 (call-saved) for the syscall number.
212 We never make function calls from inside here (only potentially
213 signal handlers), so we do not bother with doubleword alignment.
215 Just like the APCS syscall convention, the EABI syscall convention uses
216 r0 through r6 for up to seven syscall arguments. None are ever passed to
217 the kernel on the stack, although incoming arguments are on the stack for
218 syscalls with five or more arguments.
220 The assembler will convert the literal pool load to a move for most
224 #define DO_CALL(syscall_name, args) \
226 ldr r7, =SYS_ify (syscall_name); \
233 str r7, [sp, #-4]!; \
234 cfi_adjust_cfa_offset (4); \
235 cfi_rel_offset (r7, 0); \
238 #define DOARGS_1 DOARGS_0
240 #define DOARGS_2 DOARGS_0
242 #define DOARGS_3 DOARGS_0
244 #define DOARGS_4 DOARGS_0
248 stmfd sp!, {r4, r7}; \
249 cfi_adjust_cfa_offset (8); \
250 cfi_rel_offset (r4, 0); \
251 cfi_rel_offset (r7, 4); \
258 stmfd sp!, {r4, r5, r7}; \
259 cfi_adjust_cfa_offset (12); \
260 cfi_rel_offset (r4, 0); \
261 cfi_rel_offset (r5, 4); \
262 cfi_rel_offset (r7, 8); \
263 .save { r4, r5, r7 }; \
269 stmfd sp!, {r4, r5, r6, r7}; \
270 cfi_adjust_cfa_offset (16); \
271 cfi_rel_offset (r4, 0); \
272 cfi_rel_offset (r5, 4); \
273 cfi_rel_offset (r6, 8); \
274 cfi_rel_offset (r7, 12); \
275 .save { r4, r5, r6, r7 }; \
276 ldmia ip, {r4, r5, r6}
281 cfi_adjust_cfa_offset (-4); \
285 #define UNDOARGS_1 UNDOARGS_0
287 #define UNDOARGS_2 UNDOARGS_0
289 #define UNDOARGS_3 UNDOARGS_0
291 #define UNDOARGS_4 UNDOARGS_0
294 ldmfd sp!, {r4, r7}; \
295 cfi_adjust_cfa_offset (-8); \
301 ldmfd sp!, {r4, r5, r7}; \
302 cfi_adjust_cfa_offset (-12); \
309 ldmfd sp!, {r4, r5, r6, r7}; \
310 cfi_adjust_cfa_offset (-16); \
317 #else /* not __ASSEMBLER__ */
319 /* Define a macro which expands into the inline wrapper code for a system
321 #undef INLINE_SYSCALL
322 #define INLINE_SYSCALL(name, nr, args...) \
323 ({ unsigned int _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
324 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0)) \
326 __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
327 _sys_result = (unsigned int) -1; \
329 (int) _sys_result; })
331 #undef INTERNAL_SYSCALL_DECL
332 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
334 #if defined(__thumb__)
335 /* We can not expose the use of r7 to the compiler. GCC (as
336 of 4.5) uses r7 as the hard frame pointer for Thumb - although
337 for Thumb-2 it isn't obviously a better choice than r11.
338 And GCC does not support asms that conflict with the frame
341 This would be easier if syscall numbers never exceeded 255,
342 but they do. For the moment the LOAD_ARGS_7 is sacrificed.
343 We can't use push/pop inside the asm because that breaks
344 unwinding (i.e. thread cancellation) for this frame. We can't
345 locally save and restore r7, because we do not know if this
346 function uses r7 or if it is our caller's r7; if it is our caller's,
347 then unwinding will fail higher up the stack. So we move the
348 syscall out of line and provide its own unwind information. */
349 # undef INTERNAL_SYSCALL_RAW
350 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
352 register int _a1 asm ("a1"); \
353 int _nametmp = name; \
354 LOAD_ARGS_##nr (args) \
355 register int _name asm ("ip") = _nametmp; \
356 asm volatile ("bl __libc_do_syscall" \
358 : "r" (_name) ASM_ARGS_##nr \
362 # undef INTERNAL_SYSCALL_RAW
363 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
365 register int _a1 asm ("r0"), _nr asm ("r7"); \
366 LOAD_ARGS_##nr (args) \
368 asm volatile ("swi 0x0 @ syscall " #name \
370 : "r" (_nr) ASM_ARGS_##nr \
375 #undef INTERNAL_SYSCALL
376 #define INTERNAL_SYSCALL(name, err, nr, args...) \
377 INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
379 #undef INTERNAL_SYSCALL_ARM
380 #define INTERNAL_SYSCALL_ARM(name, err, nr, args...) \
381 INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
383 #undef INTERNAL_SYSCALL_ERROR_P
384 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
385 ((unsigned int) (val) >= 0xfffff001u)
387 #undef INTERNAL_SYSCALL_ERRNO
388 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
390 #define LOAD_ARGS_0()
392 #define LOAD_ARGS_1(a1) \
393 int _a1tmp = (int) (a1); \
396 #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
397 #define LOAD_ARGS_2(a1, a2) \
398 int _a2tmp = (int) (a2); \
400 register int _a2 asm ("a2") = _a2tmp;
401 #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
402 #define LOAD_ARGS_3(a1, a2, a3) \
403 int _a3tmp = (int) (a3); \
404 LOAD_ARGS_2 (a1, a2) \
405 register int _a3 asm ("a3") = _a3tmp;
406 #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
407 #define LOAD_ARGS_4(a1, a2, a3, a4) \
408 int _a4tmp = (int) (a4); \
409 LOAD_ARGS_3 (a1, a2, a3) \
410 register int _a4 asm ("a4") = _a4tmp;
411 #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
412 #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
413 int _v1tmp = (int) (a5); \
414 LOAD_ARGS_4 (a1, a2, a3, a4) \
415 register int _v1 asm ("v1") = _v1tmp;
416 #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
417 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
418 int _v2tmp = (int) (a6); \
419 LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
420 register int _v2 asm ("v2") = _v2tmp;
421 #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
423 # define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
424 int _v3tmp = (int) (a7); \
425 LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6) \
426 register int _v3 asm ("v3") = _v3tmp;
427 # define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
430 /* For EABI, non-constant syscalls are actually pretty easy... */
431 #undef INTERNAL_SYSCALL_NCS
432 #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
433 INTERNAL_SYSCALL_RAW (number, err, nr, args)
435 #endif /* __ASSEMBLER__ */
437 /* Pointer mangling is not yet supported for ARM. */
438 #define PTR_MANGLE(var) (void) (var)
439 #define PTR_DEMANGLE(var) (void) (var)
441 #endif /* linux/arm/sysdep.h */