arm: Introduce and use GET_TLS
[glibc.git] / ports / sysdeps / unix / sysv / linux / arm / sysdep.h
blobdc2058b275ca4334700ad05d8510991655a363e1
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 ldr lr, [sp], #4; \
151 cfi_adjust_cfa_offset (-4); \
152 cfi_restore (lr); \
153 bx lr
154 # else
155 # define POP_PC \
156 ldr pc, [sp], #4
157 # endif
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)); \
166 ldr r1, [sp], #4; \
167 cfi_adjust_cfa_offset (-4); \
168 rsb r1, r1, #0; \
169 str r1, [r0]; \
170 mvn r0, #0; \
171 POP_PC;
172 # endif
173 #else
174 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
175 # define SYSCALL_ERROR __syscall_error
176 #endif
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
183 may be invoked. */
185 /* Linux takes system call args in registers:
186 arg 1 r0
187 arg 2 r1
188 arg 3 r2
189 arg 4 r3
190 arg 5 r4 (this is different from the APCS convention)
191 arg 6 r5
192 arg 7 r6
194 The compiler is going to form a call by coming here, through PSEUDO, with
195 arguments
196 syscall number in the DO_CALL macro
197 arg 1 r0
198 arg 2 r1
199 arg 3 r2
200 arg 4 r3
201 arg 5 [sp]
202 arg 6 [sp+4]
203 arg 7 [sp+8]
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
221 syscalls. */
223 #undef DO_CALL
224 #define DO_CALL(syscall_name, args) \
225 DOARGS_##args; \
226 ldr r7, =SYS_ify (syscall_name); \
227 swi 0x0; \
228 UNDOARGS_##args
230 #undef DOARGS_0
231 #define DOARGS_0 \
232 .fnstart; \
233 str r7, [sp, #-4]!; \
234 cfi_adjust_cfa_offset (4); \
235 cfi_rel_offset (r7, 0); \
236 .save { r7 }
237 #undef DOARGS_1
238 #define DOARGS_1 DOARGS_0
239 #undef DOARGS_2
240 #define DOARGS_2 DOARGS_0
241 #undef DOARGS_3
242 #define DOARGS_3 DOARGS_0
243 #undef DOARGS_4
244 #define DOARGS_4 DOARGS_0
245 #undef DOARGS_5
246 #define DOARGS_5 \
247 .fnstart; \
248 stmfd sp!, {r4, r7}; \
249 cfi_adjust_cfa_offset (8); \
250 cfi_rel_offset (r4, 0); \
251 cfi_rel_offset (r7, 4); \
252 .save { r4, r7 }; \
253 ldr r4, [sp, #8]
254 #undef DOARGS_6
255 #define DOARGS_6 \
256 .fnstart; \
257 mov ip, sp; \
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 }; \
264 ldmia ip, {r4, r5}
265 #undef DOARGS_7
266 #define DOARGS_7 \
267 .fnstart; \
268 mov ip, sp; \
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}
278 #undef UNDOARGS_0
279 #define UNDOARGS_0 \
280 ldr r7, [sp], #4; \
281 cfi_adjust_cfa_offset (-4); \
282 cfi_restore (r7); \
283 .fnend
284 #undef UNDOARGS_1
285 #define UNDOARGS_1 UNDOARGS_0
286 #undef UNDOARGS_2
287 #define UNDOARGS_2 UNDOARGS_0
288 #undef UNDOARGS_3
289 #define UNDOARGS_3 UNDOARGS_0
290 #undef UNDOARGS_4
291 #define UNDOARGS_4 UNDOARGS_0
292 #undef UNDOARGS_5
293 #define UNDOARGS_5 \
294 ldmfd sp!, {r4, r7}; \
295 cfi_adjust_cfa_offset (-8); \
296 cfi_restore (r4); \
297 cfi_restore (r7); \
298 .fnend
299 #undef UNDOARGS_6
300 #define UNDOARGS_6 \
301 ldmfd sp!, {r4, r5, r7}; \
302 cfi_adjust_cfa_offset (-12); \
303 cfi_restore (r4); \
304 cfi_restore (r5); \
305 cfi_restore (r7); \
306 .fnend
307 #undef UNDOARGS_7
308 #define UNDOARGS_7 \
309 ldmfd sp!, {r4, r5, r6, r7}; \
310 cfi_adjust_cfa_offset (-16); \
311 cfi_restore (r4); \
312 cfi_restore (r5); \
313 cfi_restore (r6); \
314 cfi_restore (r7); \
315 .fnend
317 #else /* not __ASSEMBLER__ */
319 /* Define a macro which expands into the inline wrapper code for a system
320 call. */
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
339 pointer.
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...) \
351 ({ \
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" \
357 : "=r" (_a1) \
358 : "r" (_name) ASM_ARGS_##nr \
359 : "memory", "lr"); \
360 _a1; })
361 #else /* ARM */
362 # undef INTERNAL_SYSCALL_RAW
363 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
364 ({ \
365 register int _a1 asm ("r0"), _nr asm ("r7"); \
366 LOAD_ARGS_##nr (args) \
367 _nr = name; \
368 asm volatile ("swi 0x0 @ syscall " #name \
369 : "=r" (_a1) \
370 : "r" (_nr) ASM_ARGS_##nr \
371 : "memory"); \
372 _a1; })
373 #endif
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()
391 #define ASM_ARGS_0
392 #define LOAD_ARGS_1(a1) \
393 int _a1tmp = (int) (a1); \
394 LOAD_ARGS_0 () \
395 _a1 = _a1tmp;
396 #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
397 #define LOAD_ARGS_2(a1, a2) \
398 int _a2tmp = (int) (a2); \
399 LOAD_ARGS_1 (a1) \
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)
422 #ifndef __thumb__
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)
428 #endif
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 */