1 /* Copyright (C) 2005-2019 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 /* Always enable vsyscalls on aarch64 */
23 #define ALWAYS_USE_VSYSCALL 1
25 #include <sysdeps/unix/sysdep.h>
26 #include <sysdeps/aarch64/sysdep.h>
27 #include <sysdeps/unix/sysv/linux/generic/sysdep.h>
29 /* Defines RTLD_PRIVATE_ERRNO and USE_DL_SYSINFO. */
30 #include <dl-sysdep.h>
34 /* In order to get __set_errno() definition in INLINE_SYSCALL. */
39 /* For Linux we can use the system call table in the header file
40 /usr/include/asm/unistd.h
41 of the kernel. But these symbols do not follow the SYS_* syntax
42 so we have to redefine the `SYS_ify' macro here. */
44 #define SYS_ify(syscall_name) (__NR_##syscall_name)
48 /* Linux uses a negative return value to indicate syscall errors,
49 unlike most Unices, which use the condition codes' carry flag.
51 Since version 2.1 the return value of a system call might be
52 negative even if the call succeeded. E.g., the `lseek' system call
53 might return a large offset. Therefore we must not anymore test
54 for < 0, but test for a real error by making sure the value in R0
55 is a real error number. Linus said he will make sure the no syscall
56 returns a value in -1 .. -4095 as a valid result so we can safely
60 # define PSEUDO(name, syscall_name, args) \
63 DO_CALL (syscall_name, args); \
68 # define PSEUDO_END(name) \
69 SYSCALL_ERROR_HANDLER \
72 # undef PSEUDO_NOERRNO
73 # define PSEUDO_NOERRNO(name, syscall_name, args) \
76 DO_CALL (syscall_name, args);
78 # undef PSEUDO_END_NOERRNO
79 # define PSEUDO_END_NOERRNO(name) \
82 # define ret_NOERRNO ret
84 /* The function has to return the error code. */
86 # define PSEUDO_ERRVAL(name, syscall_name, args) \
89 DO_CALL (syscall_name, args); \
92 # undef PSEUDO_END_ERRVAL
93 # define PSEUDO_END_ERRVAL(name) \
96 # define ret_ERRVAL ret
99 # define SYSCALL_ERROR .Lsyscall_error
100 # if RTLD_PRIVATE_ERRNO
101 # define SYSCALL_ERROR_HANDLER \
103 adrp x1, C_SYMBOL_NAME(rtld_errno); \
105 str w0, [x1, :lo12:C_SYMBOL_NAME(rtld_errno)]; \
110 # define SYSCALL_ERROR_HANDLER \
112 adrp x1, :gottprel:errno; \
114 ldr PTR_REG(1), [x1, :gottprel_lo12:errno]; \
121 # define SYSCALL_ERROR __syscall_error
122 # define SYSCALL_ERROR_HANDLER \
127 /* Linux takes system call args in registers:
137 The compiler is going to form a call by coming here, through PSEUDO, with
139 syscall number in the DO_CALL macro
151 # define DO_CALL(syscall_name, args) \
152 mov x8, SYS_ify (syscall_name); \
155 #else /* not __ASSEMBLER__ */
158 /* List of system calls which are supported as vsyscalls. */
159 # define HAVE_CLOCK_GETRES_VSYSCALL 1
160 # define HAVE_CLOCK_GETTIME_VSYSCALL 1
161 # define HAVE_GETTIMEOFDAY_VSYSCALL 1
163 /* Previously AArch64 used the generic version without the libc_hidden_def
164 which lead in a non existent __send symbol in libc.so. */
165 # undef HAVE_INTERNAL_SEND_SYMBOL
167 # define SINGLE_THREAD_BY_GLOBAL 1
169 /* Define a macro which expands into the inline wrapper code for a system
171 # undef INLINE_SYSCALL
172 # define INLINE_SYSCALL(name, nr, args...) \
173 ({ unsigned long _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
174 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0))\
176 __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
177 _sys_result = (unsigned long) -1; \
179 (long) _sys_result; })
181 # undef INTERNAL_SYSCALL_DECL
182 # define INTERNAL_SYSCALL_DECL(err) do { } while (0)
184 # undef INTERNAL_SYSCALL_RAW
185 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
186 ({ long _sys_result; \
188 LOAD_ARGS_##nr (args) \
189 register long _x8 asm ("x8") = (name); \
190 asm volatile ("svc 0 // syscall " # name \
191 : "=r" (_x0) : "r"(_x8) ASM_ARGS_##nr : "memory"); \
196 # undef INTERNAL_SYSCALL
197 # define INTERNAL_SYSCALL(name, err, nr, args...) \
198 INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
200 # undef INTERNAL_SYSCALL_AARCH64
201 # define INTERNAL_SYSCALL_AARCH64(name, err, nr, args...) \
202 INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
204 # undef INTERNAL_SYSCALL_ERROR_P
205 # define INTERNAL_SYSCALL_ERROR_P(val, err) \
206 ((unsigned long) (val) >= (unsigned long) -4095)
208 # undef INTERNAL_SYSCALL_ERRNO
209 # define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
211 # define LOAD_ARGS_0() \
212 register long _x0 asm ("x0");
213 # define LOAD_ARGS_1(x0) \
214 long _x0tmp = (long) (x0); \
217 # define LOAD_ARGS_2(x0, x1) \
218 long _x1tmp = (long) (x1); \
220 register long _x1 asm ("x1") = _x1tmp;
221 # define LOAD_ARGS_3(x0, x1, x2) \
222 long _x2tmp = (long) (x2); \
223 LOAD_ARGS_2 (x0, x1) \
224 register long _x2 asm ("x2") = _x2tmp;
225 # define LOAD_ARGS_4(x0, x1, x2, x3) \
226 long _x3tmp = (long) (x3); \
227 LOAD_ARGS_3 (x0, x1, x2) \
228 register long _x3 asm ("x3") = _x3tmp;
229 # define LOAD_ARGS_5(x0, x1, x2, x3, x4) \
230 long _x4tmp = (long) (x4); \
231 LOAD_ARGS_4 (x0, x1, x2, x3) \
232 register long _x4 asm ("x4") = _x4tmp;
233 # define LOAD_ARGS_6(x0, x1, x2, x3, x4, x5) \
234 long _x5tmp = (long) (x5); \
235 LOAD_ARGS_5 (x0, x1, x2, x3, x4) \
236 register long _x5 asm ("x5") = _x5tmp;
237 # define LOAD_ARGS_7(x0, x1, x2, x3, x4, x5, x6)\
238 long _x6tmp = (long) (x6); \
239 LOAD_ARGS_6 (x0, x1, x2, x3, x4, x5) \
240 register long _x6 asm ("x6") = _x6tmp;
243 # define ASM_ARGS_1 , "r" (_x0)
244 # define ASM_ARGS_2 ASM_ARGS_1, "r" (_x1)
245 # define ASM_ARGS_3 ASM_ARGS_2, "r" (_x2)
246 # define ASM_ARGS_4 ASM_ARGS_3, "r" (_x3)
247 # define ASM_ARGS_5 ASM_ARGS_4, "r" (_x4)
248 # define ASM_ARGS_6 ASM_ARGS_5, "r" (_x5)
249 # define ASM_ARGS_7 ASM_ARGS_6, "r" (_x6)
251 # undef INTERNAL_SYSCALL_NCS
252 # define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
253 INTERNAL_SYSCALL_RAW (number, err, nr, args)
255 #endif /* __ASSEMBLER__ */
257 /* Pointer mangling is supported for AArch64. */
258 #if (IS_IN (rtld) || \
259 (!defined SHARED && (IS_IN (libc) \
260 || IS_IN (libpthread))))
261 # ifdef __ASSEMBLER__
262 /* Note, dst, src, guard, and tmp are all register numbers rather than
263 register names so they will work with both ILP32 and LP64. */
264 # define PTR_MANGLE(dst, src, guard, tmp) \
265 LDST_PCREL (ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard_local)); \
266 PTR_MANGLE2 (dst, src, guard)
267 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
268 # define PTR_MANGLE2(dst, src, guard)\
269 eor x##dst, x##src, x##guard
270 # define PTR_DEMANGLE(dst, src, guard, tmp)\
271 PTR_MANGLE (dst, src, guard, tmp)
272 # define PTR_DEMANGLE2(dst, src, guard)\
273 PTR_MANGLE2 (dst, src, guard)
275 extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden
;
276 # define PTR_MANGLE(var) \
277 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
278 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
281 # ifdef __ASSEMBLER__
282 /* Note, dst, src, guard, and tmp are all register numbers rather than
283 register names so they will work with both ILP32 and LP64. */
284 # define PTR_MANGLE(dst, src, guard, tmp) \
285 LDST_GLOBAL (ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard)); \
286 PTR_MANGLE2 (dst, src, guard)
287 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
288 # define PTR_MANGLE2(dst, src, guard)\
289 eor x##dst, x##src, x##guard
290 # define PTR_DEMANGLE(dst, src, guard, tmp)\
291 PTR_MANGLE (dst, src, guard, tmp)
292 # define PTR_DEMANGLE2(dst, src, guard)\
293 PTR_MANGLE2 (dst, src, guard)
295 extern uintptr_t __pointer_chk_guard attribute_relro
;
296 # define PTR_MANGLE(var) \
297 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard)
298 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
302 #endif /* linux/aarch64/sysdep.h */