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