1 /* Copyright (C) 2005-2015 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>
31 /* In order to get __set_errno() definition in INLINE_SYSCALL. */
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. */
41 #define SYS_ify(syscall_name) (__NR_##syscall_name)
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
57 # define PSEUDO(name, syscall_name, args) \
60 DO_CALL (syscall_name, args); \
65 # define PSEUDO_END(name) \
66 SYSCALL_ERROR_HANDLER \
69 # undef PSEUDO_NOERRNO
70 # define PSEUDO_NOERRNO(name, syscall_name, args) \
73 DO_CALL (syscall_name, args);
75 # undef PSEUDO_END_NOERRNO
76 # define PSEUDO_END_NOERRNO(name) \
79 # define ret_NOERRNO ret
81 /* The function has to return the error code. */
83 # define PSEUDO_ERRVAL(name, syscall_name, args) \
86 DO_CALL (syscall_name, args); \
89 # undef PSEUDO_END_ERRVAL
90 # define PSEUDO_END_ERRVAL(name) \
93 # define ret_ERRVAL ret
96 # define SYSCALL_ERROR .Lsyscall_error
97 # if RTLD_PRIVATE_ERRNO
98 # define SYSCALL_ERROR_HANDLER \
100 adrp x1, C_SYMBOL_NAME(rtld_errno); \
102 str w0, [x1, :lo12:C_SYMBOL_NAME(rtld_errno)]; \
107 # define SYSCALL_ERROR_HANDLER \
109 adrp x1, :gottprel:errno; \
111 ldr x1, [x1, :gottprel_lo12:errno]; \
118 # define SYSCALL_ERROR __syscall_error
119 # define SYSCALL_ERROR_HANDLER \
124 /* Linux takes system call args in registers:
134 The compiler is going to form a call by coming here, through PSEUDO, with
136 syscall number in the DO_CALL macro
148 # define DO_CALL(syscall_name, args) \
149 mov x8, SYS_ify (syscall_name); \
152 #else /* not __ASSEMBLER__ */
155 /* List of system calls which are supported as vsyscalls. */
156 # define HAVE_CLOCK_GETRES_VSYSCALL 1
157 # define HAVE_CLOCK_GETTIME_VSYSCALL 1
159 /* Define a macro which expands into the inline wrapper code for a system
161 # undef INLINE_SYSCALL
162 # define INLINE_SYSCALL(name, nr, args...) \
163 ({ unsigned long _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
164 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0))\
166 __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
167 _sys_result = (unsigned long) -1; \
169 (long) _sys_result; })
171 # undef INTERNAL_SYSCALL_DECL
172 # define INTERNAL_SYSCALL_DECL(err) do { } while (0)
174 # undef INTERNAL_SYSCALL_RAW
175 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
176 ({ long _sys_result; \
178 LOAD_ARGS_##nr (args) \
179 register long _x8 asm ("x8") = (name); \
180 asm volatile ("svc 0 // syscall " # name \
181 : "=r" (_x0) : "r"(_x8) ASM_ARGS_##nr : "memory"); \
186 # undef INTERNAL_SYSCALL
187 # define INTERNAL_SYSCALL(name, err, nr, args...) \
188 INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
190 # undef INTERNAL_SYSCALL_AARCH64
191 # define INTERNAL_SYSCALL_AARCH64(name, err, nr, args...) \
192 INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
194 # undef INTERNAL_SYSCALL_ERROR_P
195 # define INTERNAL_SYSCALL_ERROR_P(val, err) \
196 ((unsigned long) (val) >= (unsigned long) -4095)
198 # undef INTERNAL_SYSCALL_ERRNO
199 # define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
201 # define LOAD_ARGS_0() \
202 register long _x0 asm ("x0");
203 # define LOAD_ARGS_1(x0) \
204 long _x0tmp = (long) (x0); \
207 # define LOAD_ARGS_2(x0, x1) \
208 long _x1tmp = (long) (x1); \
210 register long _x1 asm ("x1") = _x1tmp;
211 # define LOAD_ARGS_3(x0, x1, x2) \
212 long _x2tmp = (long) (x2); \
213 LOAD_ARGS_2 (x0, x1) \
214 register long _x2 asm ("x2") = _x2tmp;
215 # define LOAD_ARGS_4(x0, x1, x2, x3) \
216 long _x3tmp = (long) (x3); \
217 LOAD_ARGS_3 (x0, x1, x2) \
218 register long _x3 asm ("x3") = _x3tmp;
219 # define LOAD_ARGS_5(x0, x1, x2, x3, x4) \
220 long _x4tmp = (long) (x4); \
221 LOAD_ARGS_4 (x0, x1, x2, x3) \
222 register long _x4 asm ("x4") = _x4tmp;
223 # define LOAD_ARGS_6(x0, x1, x2, x3, x4, x5) \
224 long _x5tmp = (long) (x5); \
225 LOAD_ARGS_5 (x0, x1, x2, x3, x4) \
226 register long _x5 asm ("x5") = _x5tmp;
227 # define LOAD_ARGS_7(x0, x1, x2, x3, x4, x5, x6)\
228 long _x6tmp = (long) (x6); \
229 LOAD_ARGS_6 (x0, x1, x2, x3, x4, x5) \
230 register long _x6 asm ("x6") = _x6tmp;
233 # define ASM_ARGS_1 , "r" (_x0)
234 # define ASM_ARGS_2 ASM_ARGS_1, "r" (_x1)
235 # define ASM_ARGS_3 ASM_ARGS_2, "r" (_x2)
236 # define ASM_ARGS_4 ASM_ARGS_3, "r" (_x3)
237 # define ASM_ARGS_5 ASM_ARGS_4, "r" (_x4)
238 # define ASM_ARGS_6 ASM_ARGS_5, "r" (_x5)
239 # define ASM_ARGS_7 ASM_ARGS_6, "r" (_x6)
241 # undef INTERNAL_SYSCALL_NCS
242 # define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
243 INTERNAL_SYSCALL_RAW (number, err, nr, args)
245 #endif /* __ASSEMBLER__ */
247 /* Pointer mangling is supported for AArch64. */
248 #if (IS_IN (rtld) || \
249 (!defined SHARED && (IS_IN (libc) \
250 || IS_IN (libpthread))))
251 # ifdef __ASSEMBLER__
252 # define PTR_MANGLE(dst, src, guard, tmp) \
253 LDST_PCREL (ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard_local)); \
254 PTR_MANGLE2 (dst, src, guard)
255 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
256 # define PTR_MANGLE2(dst, src, guard)\
258 # define PTR_DEMANGLE(dst, src, guard, tmp)\
259 PTR_MANGLE (dst, src, guard, tmp)
260 # define PTR_DEMANGLE2(dst, src, guard)\
261 PTR_MANGLE2 (dst, src, guard)
263 extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden
;
264 # define PTR_MANGLE(var) \
265 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
266 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
269 # ifdef __ASSEMBLER__
270 # define PTR_MANGLE(dst, src, guard, tmp) \
271 LDST_GLOBAL (ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard)); \
272 PTR_MANGLE2 (dst, src, guard)
273 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
274 # define PTR_MANGLE2(dst, src, guard)\
276 # define PTR_DEMANGLE(dst, src, guard, tmp)\
277 PTR_MANGLE (dst, src, guard, tmp)
278 # define PTR_DEMANGLE2(dst, src, guard)\
279 PTR_MANGLE2 (dst, src, guard)
281 extern uintptr_t __pointer_chk_guard attribute_relro
;
282 # define PTR_MANGLE(var) \
283 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard)
284 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
288 #endif /* linux/aarch64/sysdep.h */