Relocate AArch64 from ports to libc.
[glibc.git] / sysdeps / unix / sysv / linux / aarch64 / sysdep.h
blob5ccf1da18b46ed71261f7f0b0c29dbb1c848d09c
1 /* Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #ifndef _LINUX_AARCH64_SYSDEP_H
20 #define _LINUX_AARCH64_SYSDEP_H 1
22 #include <sysdeps/unix/sysdep.h>
23 #include <sysdeps/aarch64/sysdep.h>
24 #include <sysdeps/unix/sysv/linux/generic/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 #ifdef __ASSEMBLER__
45 /* Linux uses a negative return value to indicate syscall errors,
46 unlike most Unices, which use the condition codes' carry flag.
48 Since version 2.1 the return value of a system call might be
49 negative even if the call succeeded. E.g., the `lseek' system call
50 might return a large offset. Therefore we must not anymore test
51 for < 0, but test for a real error by making sure the value in R0
52 is a real error number. Linus said he will make sure the no syscall
53 returns a value in -1 .. -4095 as a valid result so we can safely
54 test with -4095. */
56 # undef PSEUDO
57 # define PSEUDO(name, syscall_name, args) \
58 .text; \
59 ENTRY (name); \
60 DO_CALL (syscall_name, args); \
61 cmn x0, #4095;
63 /* Notice the use of 'RET' instead of 'ret' the assembler is case
64 insensitive and eglibc already uses the preprocessor symbol 'ret'
65 so we use the upper case 'RET' to force through a ret instruction
66 to the assembler */
67 # define PSEUDO_RET \
68 b.cs 1f; \
69 RET; \
70 1: \
71 b SYSCALL_ERROR
72 # undef ret
73 # define ret PSEUDO_RET
75 # undef PSEUDO_END
76 # define PSEUDO_END(name) \
77 SYSCALL_ERROR_HANDLER \
78 END (name)
80 # undef PSEUDO_NOERRNO
81 # define PSEUDO_NOERRNO(name, syscall_name, args) \
82 .text; \
83 ENTRY (name); \
84 DO_CALL (syscall_name, args);
86 /* Notice the use of 'RET' instead of 'ret' the assembler is case
87 insensitive and eglibc already uses the preprocessor symbol 'ret'
88 so we use the upper case 'RET' to force through a ret instruction
89 to the assembler */
90 # define PSEUDO_RET_NOERRNO \
91 RET;
93 # undef ret_NOERRNO
94 # define ret_NOERRNO PSEUDO_RET_NOERRNO
96 # undef PSEUDO_END_NOERRNO
97 # define PSEUDO_END_NOERRNO(name) \
98 END (name)
100 /* The function has to return the error code. */
101 # undef PSEUDO_ERRVAL
102 # define PSEUDO_ERRVAL(name, syscall_name, args) \
103 .text; \
104 ENTRY (name) \
105 DO_CALL (syscall_name, args); \
106 neg x0, x0
108 # undef PSEUDO_END_ERRVAL
109 # define PSEUDO_END_ERRVAL(name) \
110 END (name)
112 # define ret_ERRVAL PSEUDO_RET_NOERRNO
114 # if NOT_IN_libc
115 # define SYSCALL_ERROR __local_syscall_error
116 # if RTLD_PRIVATE_ERRNO
117 # define SYSCALL_ERROR_HANDLER \
118 __local_syscall_error: \
119 adrp x1, C_SYMBOL_NAME(rtld_errno); \
120 add x1, x1, #:lo12:C_SYMBOL_NAME(rtld_errno); \
121 neg w0, w0; \
122 str w0, [x1]; \
123 mov x0, -1; \
124 RET;
125 # else
127 # define SYSCALL_ERROR_HANDLER \
128 __local_syscall_error: \
129 stp x29, x30, [sp, -32]!; \
130 cfi_adjust_cfa_offset (32); \
131 cfi_rel_offset (x29, 0); \
132 cfi_rel_offset (x30, 8); \
133 add x29, sp, 0; \
134 str x19, [sp,16]; \
135 neg x19, x0; \
136 bl C_SYMBOL_NAME(__errno_location); \
137 str w19, [x0]; \
138 mov x0, -1; \
139 ldr x19, [sp,16]; \
140 ldp x29, x30, [sp], 32; \
141 cfi_adjust_cfa_offset (-32); \
142 cfi_restore (x29); \
143 cfi_restore (x30); \
144 RET;
145 # endif
146 # else
147 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
148 # define SYSCALL_ERROR __syscall_error
149 # endif
151 /* Linux takes system call args in registers:
152 syscall number in the SVC instruction
153 arg 1 x0
154 arg 2 x1
155 arg 3 x2
156 arg 4 x3
157 arg 5 x4
158 arg 6 x5
159 arg 7 x6
161 The compiler is going to form a call by coming here, through PSEUDO, with
162 arguments
163 syscall number in the DO_CALL macro
164 arg 1 x0
165 arg 2 x1
166 arg 3 x2
167 arg 4 x3
168 arg 5 x4
169 arg 6 x5
170 arg 7 x6
172 We need to shuffle values between R4..R6 and the stack so that the
173 caller's v1..v3 and stack frame are not corrupted, and the kernel
174 sees the right arguments.
178 # undef DO_CALL
179 # define DO_CALL(syscall_name, args) \
180 DOARGS_##args \
181 mov x8, SYS_ify (syscall_name); \
182 svc 0; \
183 UNDOARGS_##args
185 # define DOARGS_0 /* nothing */
186 # define DOARGS_1 /* nothing */
187 # define DOARGS_2 /* nothing */
188 # define DOARGS_3 /* nothing */
189 # define DOARGS_4 /* nothing */
190 # define DOARGS_5 /* nothing */
191 # define DOARGS_6 /* nothing */
192 # define DOARGS_7 /* nothing */
194 # define UNDOARGS_0 /* nothing */
195 # define UNDOARGS_1 /* nothing */
196 # define UNDOARGS_2 /* nothing */
197 # define UNDOARGS_3 /* nothing */
198 # define UNDOARGS_4 /* nothing */
199 # define UNDOARGS_5 /* nothing */
200 # define UNDOARGS_6 /* nothing */
201 # define UNDOARGS_7 /* nothing */
203 #else /* not __ASSEMBLER__ */
205 # ifdef SHARED
206 # define INLINE_VSYSCALL(name, nr, args...) \
207 ({ \
208 __label__ out; \
209 __label__ iserr; \
210 long sc_ret; \
211 INTERNAL_SYSCALL_DECL (sc_err); \
213 if (__vdso_##name != NULL) \
215 sc_ret = INTERNAL_VSYSCALL_NCS (__vdso_##name, sc_err, nr, ##args); \
216 if (!INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
217 goto out; \
218 if (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err) != ENOSYS) \
219 goto iserr; \
222 sc_ret = INTERNAL_SYSCALL (name, sc_err, nr, ##args); \
223 if (INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
225 iserr: \
226 __set_errno (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err)); \
227 sc_ret = -1L; \
229 out: \
230 sc_ret; \
232 # else
233 # define INLINE_VSYSCALL(name, nr, args...) \
234 INLINE_SYSCALL (name, nr, ##args)
235 # endif
237 # ifdef SHARED
238 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
239 ({ \
240 __label__ out; \
241 long v_ret; \
243 if (__vdso_##name != NULL) \
245 v_ret = INTERNAL_VSYSCALL_NCS (__vdso_##name, err, nr, ##args); \
246 if (!INTERNAL_SYSCALL_ERROR_P (v_ret, err) \
247 || INTERNAL_SYSCALL_ERRNO (v_ret, err) != ENOSYS) \
248 goto out; \
250 v_ret = INTERNAL_SYSCALL (name, err, nr, ##args); \
251 out: \
252 v_ret; \
254 # else
255 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
256 INTERNAL_SYSCALL (name, err, nr, ##args)
257 # endif
259 /* List of system calls which are supported as vsyscalls. */
260 # define HAVE_CLOCK_GETRES_VSYSCALL 1
261 # define HAVE_CLOCK_GETTIME_VSYSCALL 1
263 # define INTERNAL_VSYSCALL_NCS(funcptr, err, nr, args...) \
264 ({ \
265 LOAD_ARGS_##nr (args) \
266 asm volatile ("blr %1" \
267 : "=r" (_x0) \
268 : "r" (funcptr), ASM_ARGS_##nr \
269 : "x30", "memory"); \
270 (long) _x0; \
274 /* Define a macro which expands into the inline wrapper code for a system
275 call. */
276 # undef INLINE_SYSCALL
277 # define INLINE_SYSCALL(name, nr, args...) \
278 ({ unsigned long _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
279 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0))\
281 __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
282 _sys_result = (unsigned long) -1; \
284 (long) _sys_result; })
286 # undef INTERNAL_SYSCALL_DECL
287 # define INTERNAL_SYSCALL_DECL(err) do { } while (0)
289 # undef INTERNAL_SYSCALL_RAW
290 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
291 ({ unsigned long _sys_result; \
293 LOAD_ARGS_##nr (args) \
294 register long _x8 asm ("x8") = (name); \
295 asm volatile ("svc 0 // syscall " # name \
296 : "+r" (_x0), "+r" (_x8) \
297 : ASM_ARGS_##nr \
298 : "memory", CLOBBER_ARGS_##nr); \
299 _sys_result = _x0; \
301 (long) _sys_result; })
303 # undef INTERNAL_SYSCALL
304 # define INTERNAL_SYSCALL(name, err, nr, args...) \
305 INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
307 # undef INTERNAL_SYSCALL_AARCH64
308 # define INTERNAL_SYSCALL_AARCH64(name, err, nr, args...) \
309 INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
311 # undef INTERNAL_SYSCALL_ERROR_P
312 # define INTERNAL_SYSCALL_ERROR_P(val, err) \
313 ((unsigned long) (val) >= (unsigned long) -4095)
315 # undef INTERNAL_SYSCALL_ERRNO
316 # define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
318 # define CLOBBER_ARGS_0 CLOBBER_ARGS_1
319 # define CLOBBER_ARGS_1 "x1", CLOBBER_ARGS_2
320 # define CLOBBER_ARGS_2 "x2", CLOBBER_ARGS_3
321 # define CLOBBER_ARGS_3 "x3", CLOBBER_ARGS_4
322 # define CLOBBER_ARGS_4 "x4", CLOBBER_ARGS_5
323 # define CLOBBER_ARGS_5 "x5", CLOBBER_ARGS_6
324 # define CLOBBER_ARGS_6 "x6", CLOBBER_ARGS_7
325 # define CLOBBER_ARGS_7 \
326 "x7", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18"
328 # define LOAD_ARGS_0() \
329 register long _x0 asm ("x0");
331 # define ASM_ARGS_0
332 # define LOAD_ARGS_1(x0) \
333 long _x0tmp = (long) (x0); \
334 LOAD_ARGS_0 () \
335 _x0 = _x0tmp;
336 # define ASM_ARGS_1 "r" (_x0)
337 # define LOAD_ARGS_2(x0, x1) \
338 long _x1tmp = (long) (x1); \
339 LOAD_ARGS_1 (x0) \
340 register long _x1 asm ("x1") = _x1tmp;
341 # define ASM_ARGS_2 ASM_ARGS_1, "r" (_x1)
342 # define LOAD_ARGS_3(x0, x1, x2) \
343 long _x2tmp = (long) (x2); \
344 LOAD_ARGS_2 (x0, x1) \
345 register long _x2 asm ("x2") = _x2tmp;
346 # define ASM_ARGS_3 ASM_ARGS_2, "r" (_x2)
347 # define LOAD_ARGS_4(x0, x1, x2, x3) \
348 long _x3tmp = (long) (x3); \
349 LOAD_ARGS_3 (x0, x1, x2) \
350 register long _x3 asm ("x3") = _x3tmp;
351 # define ASM_ARGS_4 ASM_ARGS_3, "r" (_x3)
352 # define LOAD_ARGS_5(x0, x1, x2, x3, x4) \
353 long _x4tmp = (long) (x4); \
354 LOAD_ARGS_4 (x0, x1, x2, x3) \
355 register long _x4 asm ("x4") = _x4tmp;
356 # define ASM_ARGS_5 ASM_ARGS_4, "r" (_x4)
357 # define LOAD_ARGS_6(x0, x1, x2, x3, x4, x5) \
358 long _x5tmp = (long) (x5); \
359 LOAD_ARGS_5 (x0, x1, x2, x3, x4) \
360 register long _x5 asm ("x5") = _x5tmp;
361 # define ASM_ARGS_6 ASM_ARGS_5, "r" (_x5)
362 # define LOAD_ARGS_7(x0, x1, x2, x3, x4, x5, x6)\
363 long _x6tmp = (long) (x6); \
364 LOAD_ARGS_6 (x0, x1, x2, x3, x4, x5) \
365 register long _x6 asm ("x6") = _x6tmp;
366 # define ASM_ARGS_7 ASM_ARGS_6, "r" (_x6)
368 # undef INTERNAL_SYSCALL_NCS
369 # define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
370 INTERNAL_SYSCALL_RAW (number, err, nr, args)
372 #endif /* __ASSEMBLER__ */
374 /* Pointer mangling is supported for AArch64. */
375 #if (defined NOT_IN_libc && defined IS_IN_rtld) || \
376 (!defined SHARED && (!defined NOT_IN_libc || defined IS_IN_libpthread))
377 # ifdef __ASSEMBLER__
378 # define PTR_MANGLE(dst, src, guard, tmp) \
379 LDST_PCREL (ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard_local)); \
380 PTR_MANGLE2 (dst, src, guard)
381 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
382 # define PTR_MANGLE2(dst, src, guard)\
383 eor dst, src, guard
384 # define PTR_DEMANGLE(dst, src, guard, tmp)\
385 PTR_MANGLE (dst, src, guard, tmp)
386 # define PTR_DEMANGLE2(dst, src, guard)\
387 PTR_MANGLE2 (dst, src, guard)
388 # else
389 extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
390 # define PTR_MANGLE(var) \
391 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
392 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
393 # endif
394 #else
395 # ifdef __ASSEMBLER__
396 # define PTR_MANGLE(dst, src, guard, tmp) \
397 LDST_GLOBAL (ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard)); \
398 PTR_MANGLE2 (dst, src, guard)
399 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
400 # define PTR_MANGLE2(dst, src, guard)\
401 eor dst, src, guard
402 # define PTR_DEMANGLE(dst, src, guard, tmp)\
403 PTR_MANGLE (dst, src, guard, tmp)
404 # define PTR_DEMANGLE2(dst, src, guard)\
405 PTR_MANGLE2 (dst, src, guard)
406 # else
407 extern uintptr_t __pointer_chk_guard attribute_relro;
408 # define PTR_MANGLE(var) \
409 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard)
410 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
411 # endif
412 #endif
414 #endif /* linux/aarch64/sysdep.h */