Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / unix / sysv / linux / arm / sysdep.h
blob11d0a1146efb48e150b8a0d9d20edded66f723a6
1 /* Copyright (C) 1992-2014 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 #ifndef ARCH_HAS_HARD_TP
49 /* Internal macro calling the linux kernel kuser_get_tls helper.
50 Note that in thumb mode, a constant pool break is often out of range, so
51 we always expand the constant inline. */
52 # ifdef __thumb2__
53 # define GET_TLS_BODY \
54 movw r0, #0x0fe0; \
55 movt r0, #0xffff; \
56 blx r0
57 # else
58 # define GET_TLS_BODY \
59 mov r0, #0xffff0fff; /* Point to the high page. */ \
60 mov lr, pc; /* Save our return address. */ \
61 sub pc, r0, #31 /* Jump to the TLS entry. */
62 # endif
64 /* Helper to get the TLS base pointer. Save LR in TMP, return in R0,
65 and no other registers clobbered. TMP may be LR itself to indicate
66 that no save is necessary. */
67 # undef GET_TLS
68 # define GET_TLS(TMP) \
69 .ifnc TMP, lr; \
70 mov TMP, lr; \
71 cfi_register (lr, TMP); \
72 GET_TLS_BODY; \
73 mov lr, TMP; \
74 cfi_restore (lr); \
75 .else; \
76 GET_TLS_BODY; \
77 .endif
78 #endif /* ARCH_HAS_HARD_TP */
80 /* Linux uses a negative return value to indicate syscall errors,
81 unlike most Unices, which use the condition codes' carry flag.
83 Since version 2.1 the return value of a system call might be
84 negative even if the call succeeded. E.g., the `lseek' system call
85 might return a large offset. Therefore we must not anymore test
86 for < 0, but test for a real error by making sure the value in R0
87 is a real error number. Linus said he will make sure the no syscall
88 returns a value in -1 .. -4095 as a valid result so we can safely
89 test with -4095. */
91 #undef PSEUDO
92 #define PSEUDO(name, syscall_name, args) \
93 .text; \
94 ENTRY (name); \
95 DO_CALL (syscall_name, args); \
96 cmn r0, $4096;
98 #define PSEUDO_RET \
99 it cc; \
100 RETINSTR(cc, lr); \
101 b PLTJMP(SYSCALL_ERROR)
102 #undef ret
103 #define ret PSEUDO_RET
105 #undef PSEUDO_END
106 #define PSEUDO_END(name) \
107 SYSCALL_ERROR_HANDLER; \
108 END (name)
110 #undef PSEUDO_NOERRNO
111 #define PSEUDO_NOERRNO(name, syscall_name, args) \
112 .text; \
113 ENTRY (name); \
114 DO_CALL (syscall_name, args);
116 #define PSEUDO_RET_NOERRNO \
117 DO_RET (lr);
119 #undef ret_NOERRNO
120 #define ret_NOERRNO PSEUDO_RET_NOERRNO
122 #undef PSEUDO_END_NOERRNO
123 #define PSEUDO_END_NOERRNO(name) \
124 END (name)
126 /* The function has to return the error code. */
127 #undef PSEUDO_ERRVAL
128 #define PSEUDO_ERRVAL(name, syscall_name, args) \
129 .text; \
130 ENTRY (name) \
131 DO_CALL (syscall_name, args); \
132 rsb r0, r0, #0
134 #undef PSEUDO_END_ERRVAL
135 #define PSEUDO_END_ERRVAL(name) \
136 END (name)
138 #define ret_ERRVAL PSEUDO_RET_NOERRNO
140 #if NOT_IN_libc
141 # define SYSCALL_ERROR __local_syscall_error
142 # if RTLD_PRIVATE_ERRNO
143 # define SYSCALL_ERROR_HANDLER \
144 __local_syscall_error: \
145 rsb r0, r0, #0; \
146 LDST_PCREL(str, r0, r1, C_SYMBOL_NAME(rtld_errno)); \
147 mvn r0, #0; \
148 DO_RET(lr)
149 # else
150 # if defined(__ARM_ARCH_4T__) && defined(__THUMB_INTERWORK__)
151 # define POP_PC \
152 pop { lr }; \
153 cfi_adjust_cfa_offset (-4); \
154 cfi_restore (lr); \
155 bx lr
156 # else
157 # define POP_PC pop { pc }
158 # endif
159 # define SYSCALL_ERROR_HANDLER \
160 __local_syscall_error: \
161 push { lr }; \
162 cfi_adjust_cfa_offset (4); \
163 cfi_rel_offset (lr, 0); \
164 push { r0 }; \
165 cfi_adjust_cfa_offset (4); \
166 bl PLTJMP(C_SYMBOL_NAME(__errno_location)); \
167 pop { r1 }; \
168 cfi_adjust_cfa_offset (-4); \
169 rsb r1, r1, #0; \
170 str r1, [r0]; \
171 mvn r0, #0; \
172 POP_PC;
173 # endif
174 #else
175 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
176 # define SYSCALL_ERROR __syscall_error
177 #endif
179 /* The ARM EABI user interface passes the syscall number in r7, instead
180 of in the swi. This is more efficient, because the kernel does not need
181 to fetch the swi from memory to find out the number; which can be painful
182 with separate I-cache and D-cache. Make sure to use 0 for the SWI
183 argument; otherwise the (optional) compatibility code for APCS binaries
184 may be invoked. */
186 /* Linux takes system call args in registers:
187 arg 1 r0
188 arg 2 r1
189 arg 3 r2
190 arg 4 r3
191 arg 5 r4 (this is different from the APCS convention)
192 arg 6 r5
193 arg 7 r6
195 The compiler is going to form a call by coming here, through PSEUDO, with
196 arguments
197 syscall number in the DO_CALL macro
198 arg 1 r0
199 arg 2 r1
200 arg 3 r2
201 arg 4 r3
202 arg 5 [sp]
203 arg 6 [sp+4]
204 arg 7 [sp+8]
206 We need to shuffle values between R4..R6 and the stack so that the
207 caller's v1..v3 and stack frame are not corrupted, and the kernel
208 sees the right arguments.
212 /* We must save and restore r7 (call-saved) for the syscall number.
213 We never make function calls from inside here (only potentially
214 signal handlers), so we do not bother with doubleword alignment.
216 Just like the APCS syscall convention, the EABI syscall convention uses
217 r0 through r6 for up to seven syscall arguments. None are ever passed to
218 the kernel on the stack, although incoming arguments are on the stack for
219 syscalls with five or more arguments.
221 The assembler will convert the literal pool load to a move for most
222 syscalls. */
224 #undef DO_CALL
225 #define DO_CALL(syscall_name, args) \
226 DOARGS_##args; \
227 ldr r7, =SYS_ify (syscall_name); \
228 swi 0x0; \
229 UNDOARGS_##args
231 #undef DOARGS_0
232 #define DOARGS_0 \
233 .fnstart; \
234 push { r7 }; \
235 cfi_adjust_cfa_offset (4); \
236 cfi_rel_offset (r7, 0); \
237 .save { r7 }
238 #undef DOARGS_1
239 #define DOARGS_1 DOARGS_0
240 #undef DOARGS_2
241 #define DOARGS_2 DOARGS_0
242 #undef DOARGS_3
243 #define DOARGS_3 DOARGS_0
244 #undef DOARGS_4
245 #define DOARGS_4 DOARGS_0
246 #undef DOARGS_5
247 #define DOARGS_5 \
248 .fnstart; \
249 push {r4, r7}; \
250 cfi_adjust_cfa_offset (8); \
251 cfi_rel_offset (r4, 0); \
252 cfi_rel_offset (r7, 4); \
253 .save { r4, r7 }; \
254 ldr r4, [sp, #8]
255 #undef DOARGS_6
256 #define DOARGS_6 \
257 .fnstart; \
258 mov ip, sp; \
259 push {r4, r5, r7}; \
260 cfi_adjust_cfa_offset (12); \
261 cfi_rel_offset (r4, 0); \
262 cfi_rel_offset (r5, 4); \
263 cfi_rel_offset (r7, 8); \
264 .save { r4, r5, r7 }; \
265 ldmia ip, {r4, r5}
266 #undef DOARGS_7
267 #define DOARGS_7 \
268 .fnstart; \
269 mov ip, sp; \
270 push {r4, r5, r6, r7}; \
271 cfi_adjust_cfa_offset (16); \
272 cfi_rel_offset (r4, 0); \
273 cfi_rel_offset (r5, 4); \
274 cfi_rel_offset (r6, 8); \
275 cfi_rel_offset (r7, 12); \
276 .save { r4, r5, r6, r7 }; \
277 ldmia ip, {r4, r5, r6}
279 #undef UNDOARGS_0
280 #define UNDOARGS_0 \
281 pop {r7}; \
282 cfi_adjust_cfa_offset (-4); \
283 cfi_restore (r7); \
284 .fnend
285 #undef UNDOARGS_1
286 #define UNDOARGS_1 UNDOARGS_0
287 #undef UNDOARGS_2
288 #define UNDOARGS_2 UNDOARGS_0
289 #undef UNDOARGS_3
290 #define UNDOARGS_3 UNDOARGS_0
291 #undef UNDOARGS_4
292 #define UNDOARGS_4 UNDOARGS_0
293 #undef UNDOARGS_5
294 #define UNDOARGS_5 \
295 pop {r4, r7}; \
296 cfi_adjust_cfa_offset (-8); \
297 cfi_restore (r4); \
298 cfi_restore (r7); \
299 .fnend
300 #undef UNDOARGS_6
301 #define UNDOARGS_6 \
302 pop {r4, r5, r7}; \
303 cfi_adjust_cfa_offset (-12); \
304 cfi_restore (r4); \
305 cfi_restore (r5); \
306 cfi_restore (r7); \
307 .fnend
308 #undef UNDOARGS_7
309 #define UNDOARGS_7 \
310 pop {r4, r5, r6, r7}; \
311 cfi_adjust_cfa_offset (-16); \
312 cfi_restore (r4); \
313 cfi_restore (r5); \
314 cfi_restore (r6); \
315 cfi_restore (r7); \
316 .fnend
318 #else /* not __ASSEMBLER__ */
320 /* Define a macro which expands into the inline wrapper code for a system
321 call. */
322 #undef INLINE_SYSCALL
323 #define INLINE_SYSCALL(name, nr, args...) \
324 ({ unsigned int _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
325 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0)) \
327 __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
328 _sys_result = (unsigned int) -1; \
330 (int) _sys_result; })
332 #undef INTERNAL_SYSCALL_DECL
333 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
335 #if defined(__thumb__)
336 /* We can not expose the use of r7 to the compiler. GCC (as
337 of 4.5) uses r7 as the hard frame pointer for Thumb - although
338 for Thumb-2 it isn't obviously a better choice than r11.
339 And GCC does not support asms that conflict with the frame
340 pointer.
342 This would be easier if syscall numbers never exceeded 255,
343 but they do. For the moment the LOAD_ARGS_7 is sacrificed.
344 We can't use push/pop inside the asm because that breaks
345 unwinding (i.e. thread cancellation) for this frame. We can't
346 locally save and restore r7, because we do not know if this
347 function uses r7 or if it is our caller's r7; if it is our caller's,
348 then unwinding will fail higher up the stack. So we move the
349 syscall out of line and provide its own unwind information. */
350 # undef INTERNAL_SYSCALL_RAW
351 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
352 ({ \
353 register int _a1 asm ("a1"); \
354 int _nametmp = name; \
355 LOAD_ARGS_##nr (args) \
356 register int _name asm ("ip") = _nametmp; \
357 asm volatile ("bl __libc_do_syscall" \
358 : "=r" (_a1) \
359 : "r" (_name) ASM_ARGS_##nr \
360 : "memory", "lr"); \
361 _a1; })
362 #else /* ARM */
363 # undef INTERNAL_SYSCALL_RAW
364 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
365 ({ \
366 register int _a1 asm ("r0"), _nr asm ("r7"); \
367 LOAD_ARGS_##nr (args) \
368 _nr = name; \
369 asm volatile ("swi 0x0 @ syscall " #name \
370 : "=r" (_a1) \
371 : "r" (_nr) ASM_ARGS_##nr \
372 : "memory"); \
373 _a1; })
374 #endif
376 #undef INTERNAL_SYSCALL
377 #define INTERNAL_SYSCALL(name, err, nr, args...) \
378 INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
380 #undef INTERNAL_SYSCALL_ARM
381 #define INTERNAL_SYSCALL_ARM(name, err, nr, args...) \
382 INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
384 #undef INTERNAL_SYSCALL_ERROR_P
385 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
386 ((unsigned int) (val) >= 0xfffff001u)
388 #undef INTERNAL_SYSCALL_ERRNO
389 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
391 #define LOAD_ARGS_0()
392 #define ASM_ARGS_0
393 #define LOAD_ARGS_1(a1) \
394 int _a1tmp = (int) (a1); \
395 LOAD_ARGS_0 () \
396 _a1 = _a1tmp;
397 #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
398 #define LOAD_ARGS_2(a1, a2) \
399 int _a2tmp = (int) (a2); \
400 LOAD_ARGS_1 (a1) \
401 register int _a2 asm ("a2") = _a2tmp;
402 #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
403 #define LOAD_ARGS_3(a1, a2, a3) \
404 int _a3tmp = (int) (a3); \
405 LOAD_ARGS_2 (a1, a2) \
406 register int _a3 asm ("a3") = _a3tmp;
407 #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
408 #define LOAD_ARGS_4(a1, a2, a3, a4) \
409 int _a4tmp = (int) (a4); \
410 LOAD_ARGS_3 (a1, a2, a3) \
411 register int _a4 asm ("a4") = _a4tmp;
412 #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
413 #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
414 int _v1tmp = (int) (a5); \
415 LOAD_ARGS_4 (a1, a2, a3, a4) \
416 register int _v1 asm ("v1") = _v1tmp;
417 #define ASM_ARGS_5 ASM_ARGS_4, "r" (_v1)
418 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
419 int _v2tmp = (int) (a6); \
420 LOAD_ARGS_5 (a1, a2, a3, a4, a5) \
421 register int _v2 asm ("v2") = _v2tmp;
422 #define ASM_ARGS_6 ASM_ARGS_5, "r" (_v2)
423 #ifndef __thumb__
424 # define LOAD_ARGS_7(a1, a2, a3, a4, a5, a6, a7) \
425 int _v3tmp = (int) (a7); \
426 LOAD_ARGS_6 (a1, a2, a3, a4, a5, a6) \
427 register int _v3 asm ("v3") = _v3tmp;
428 # define ASM_ARGS_7 ASM_ARGS_6, "r" (_v3)
429 #endif
431 /* For EABI, non-constant syscalls are actually pretty easy... */
432 #undef INTERNAL_SYSCALL_NCS
433 #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
434 INTERNAL_SYSCALL_RAW (number, err, nr, args)
436 #endif /* __ASSEMBLER__ */
438 /* Pointer mangling support. */
439 #if (defined NOT_IN_libc && defined IS_IN_rtld) || \
440 (!defined SHARED && (!defined NOT_IN_libc || defined IS_IN_libpthread))
441 # ifdef __ASSEMBLER__
442 # define PTR_MANGLE(dst, src, guard, tmp) \
443 LDST_PCREL(ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard_local)); \
444 PTR_MANGLE2(dst, src, guard)
445 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
446 # define PTR_MANGLE2(dst, src, guard) \
447 eor dst, src, guard
448 # define PTR_DEMANGLE(dst, src, guard, tmp) \
449 PTR_MANGLE (dst, src, guard, tmp)
450 # define PTR_DEMANGLE2(dst, src, guard) \
451 PTR_MANGLE2 (dst, src, guard)
452 # else
453 extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
454 # define PTR_MANGLE(var) \
455 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
456 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
457 # endif
458 #else
459 # ifdef __ASSEMBLER__
460 # define PTR_MANGLE(dst, src, guard, tmp) \
461 LDST_GLOBAL(ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard)); \
462 PTR_MANGLE2(dst, src, guard)
463 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
464 # define PTR_MANGLE2(dst, src, guard) \
465 eor dst, src, guard
466 # define PTR_DEMANGLE(dst, src, guard, tmp) \
467 PTR_MANGLE (dst, src, guard, tmp)
468 # define PTR_DEMANGLE2(dst, src, guard) \
469 PTR_MANGLE2 (dst, src, guard)
470 # else
471 extern uintptr_t __pointer_chk_guard attribute_relro;
472 # define PTR_MANGLE(var) \
473 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard)
474 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
475 # endif
476 #endif
478 #endif /* linux/arm/sysdep.h */