Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / aarch64 / sysdep.h
blob91e03fcd6fcb1abd7fbe3fed3ffe5650c7ff66ff
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>
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; \
62 b.cs .Lsyscall_error;
64 # undef PSEUDO_END
65 # define PSEUDO_END(name) \
66 SYSCALL_ERROR_HANDLER \
67 END (name)
69 # undef PSEUDO_NOERRNO
70 # define PSEUDO_NOERRNO(name, syscall_name, args) \
71 .text; \
72 ENTRY (name); \
73 DO_CALL (syscall_name, args);
75 # undef PSEUDO_END_NOERRNO
76 # define PSEUDO_END_NOERRNO(name) \
77 END (name)
79 # define ret_NOERRNO ret
81 /* The function has to return the error code. */
82 # undef PSEUDO_ERRVAL
83 # define PSEUDO_ERRVAL(name, syscall_name, args) \
84 .text; \
85 ENTRY (name) \
86 DO_CALL (syscall_name, args); \
87 neg x0, x0
89 # undef PSEUDO_END_ERRVAL
90 # define PSEUDO_END_ERRVAL(name) \
91 END (name)
93 # define ret_ERRVAL ret
95 # if !IS_IN (libc)
96 # define SYSCALL_ERROR .Lsyscall_error
97 # if RTLD_PRIVATE_ERRNO
98 # define SYSCALL_ERROR_HANDLER \
99 .Lsyscall_error: \
100 adrp x1, C_SYMBOL_NAME(rtld_errno); \
101 neg w0, w0; \
102 str w0, [x1, :lo12:C_SYMBOL_NAME(rtld_errno)]; \
103 mov x0, -1; \
104 RET;
105 # else
107 # define SYSCALL_ERROR_HANDLER \
108 .Lsyscall_error: \
109 adrp x1, :gottprel:errno; \
110 neg w2, w0; \
111 ldr x1, [x1, :gottprel_lo12:errno]; \
112 mrs x3, tpidr_el0; \
113 mov x0, -1; \
114 str w2, [x1, x3]; \
115 RET;
116 # endif
117 # else
118 # define SYSCALL_ERROR __syscall_error
119 # define SYSCALL_ERROR_HANDLER \
120 .Lsyscall_error: \
121 b __syscall_error;
122 # endif
124 /* Linux takes system call args in registers:
125 syscall number x8
126 arg 1 x0
127 arg 2 x1
128 arg 3 x2
129 arg 4 x3
130 arg 5 x4
131 arg 6 x5
132 arg 7 x6
134 The compiler is going to form a call by coming here, through PSEUDO, with
135 arguments
136 syscall number in the DO_CALL macro
137 arg 1 x0
138 arg 2 x1
139 arg 3 x2
140 arg 4 x3
141 arg 5 x4
142 arg 6 x5
143 arg 7 x6
147 # undef DO_CALL
148 # define DO_CALL(syscall_name, args) \
149 mov x8, SYS_ify (syscall_name); \
150 svc 0
152 #else /* not __ASSEMBLER__ */
154 # ifdef SHARED
155 # define INLINE_VSYSCALL(name, nr, args...) \
156 ({ \
157 __label__ out; \
158 __label__ iserr; \
159 long sc_ret; \
160 INTERNAL_SYSCALL_DECL (sc_err); \
162 if (__vdso_##name != NULL) \
164 sc_ret = INTERNAL_VSYSCALL_NCS (__vdso_##name, sc_err, nr, ##args); \
165 if (!INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
166 goto out; \
167 if (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err) != ENOSYS) \
168 goto iserr; \
171 sc_ret = INTERNAL_SYSCALL (name, sc_err, nr, ##args); \
172 if (INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
174 iserr: \
175 __set_errno (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err)); \
176 sc_ret = -1L; \
178 out: \
179 sc_ret; \
181 # else
182 # define INLINE_VSYSCALL(name, nr, args...) \
183 INLINE_SYSCALL (name, nr, ##args)
184 # endif
186 # ifdef SHARED
187 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
188 ({ \
189 __label__ out; \
190 long v_ret; \
192 if (__vdso_##name != NULL) \
194 v_ret = INTERNAL_VSYSCALL_NCS (__vdso_##name, err, nr, ##args); \
195 if (!INTERNAL_SYSCALL_ERROR_P (v_ret, err) \
196 || INTERNAL_SYSCALL_ERRNO (v_ret, err) != ENOSYS) \
197 goto out; \
199 v_ret = INTERNAL_SYSCALL (name, err, nr, ##args); \
200 out: \
201 v_ret; \
203 # else
204 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
205 INTERNAL_SYSCALL (name, err, nr, ##args)
206 # endif
208 /* List of system calls which are supported as vsyscalls. */
209 # define HAVE_CLOCK_GETRES_VSYSCALL 1
210 # define HAVE_CLOCK_GETTIME_VSYSCALL 1
212 # define INTERNAL_VSYSCALL_NCS(funcptr, err, nr, args...) \
213 ({ \
214 LOAD_ARGS_##nr (args) \
215 asm volatile ("blr %1" \
216 : "=r" (_x0) \
217 : "r" (funcptr) ASM_ARGS_##nr \
218 : "x30", "memory"); \
219 (long) _x0; \
223 /* Define a macro which expands into the inline wrapper code for a system
224 call. */
225 # undef INLINE_SYSCALL
226 # define INLINE_SYSCALL(name, nr, args...) \
227 ({ unsigned long _sys_result = INTERNAL_SYSCALL (name, , nr, args); \
228 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_sys_result, ), 0))\
230 __set_errno (INTERNAL_SYSCALL_ERRNO (_sys_result, )); \
231 _sys_result = (unsigned long) -1; \
233 (long) _sys_result; })
235 # undef INTERNAL_SYSCALL_DECL
236 # define INTERNAL_SYSCALL_DECL(err) do { } while (0)
238 # undef INTERNAL_SYSCALL_RAW
239 # define INTERNAL_SYSCALL_RAW(name, err, nr, args...) \
240 ({ long _sys_result; \
242 LOAD_ARGS_##nr (args) \
243 register long _x8 asm ("x8") = (name); \
244 asm volatile ("svc 0 // syscall " # name \
245 : "=r" (_x0) : "r"(_x8) ASM_ARGS_##nr : "memory"); \
246 _sys_result = _x0; \
248 _sys_result; })
250 # undef INTERNAL_SYSCALL
251 # define INTERNAL_SYSCALL(name, err, nr, args...) \
252 INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
254 # undef INTERNAL_SYSCALL_AARCH64
255 # define INTERNAL_SYSCALL_AARCH64(name, err, nr, args...) \
256 INTERNAL_SYSCALL_RAW(__ARM_NR_##name, err, nr, args)
258 # undef INTERNAL_SYSCALL_ERROR_P
259 # define INTERNAL_SYSCALL_ERROR_P(val, err) \
260 ((unsigned long) (val) >= (unsigned long) -4095)
262 # undef INTERNAL_SYSCALL_ERRNO
263 # define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
265 # define LOAD_ARGS_0() \
266 register long _x0 asm ("x0");
267 # define LOAD_ARGS_1(x0) \
268 long _x0tmp = (long) (x0); \
269 LOAD_ARGS_0 () \
270 _x0 = _x0tmp;
271 # define LOAD_ARGS_2(x0, x1) \
272 long _x1tmp = (long) (x1); \
273 LOAD_ARGS_1 (x0) \
274 register long _x1 asm ("x1") = _x1tmp;
275 # define LOAD_ARGS_3(x0, x1, x2) \
276 long _x2tmp = (long) (x2); \
277 LOAD_ARGS_2 (x0, x1) \
278 register long _x2 asm ("x2") = _x2tmp;
279 # define LOAD_ARGS_4(x0, x1, x2, x3) \
280 long _x3tmp = (long) (x3); \
281 LOAD_ARGS_3 (x0, x1, x2) \
282 register long _x3 asm ("x3") = _x3tmp;
283 # define LOAD_ARGS_5(x0, x1, x2, x3, x4) \
284 long _x4tmp = (long) (x4); \
285 LOAD_ARGS_4 (x0, x1, x2, x3) \
286 register long _x4 asm ("x4") = _x4tmp;
287 # define LOAD_ARGS_6(x0, x1, x2, x3, x4, x5) \
288 long _x5tmp = (long) (x5); \
289 LOAD_ARGS_5 (x0, x1, x2, x3, x4) \
290 register long _x5 asm ("x5") = _x5tmp;
291 # define LOAD_ARGS_7(x0, x1, x2, x3, x4, x5, x6)\
292 long _x6tmp = (long) (x6); \
293 LOAD_ARGS_6 (x0, x1, x2, x3, x4, x5) \
294 register long _x6 asm ("x6") = _x6tmp;
296 # define ASM_ARGS_0
297 # define ASM_ARGS_1 , "r" (_x0)
298 # define ASM_ARGS_2 ASM_ARGS_1, "r" (_x1)
299 # define ASM_ARGS_3 ASM_ARGS_2, "r" (_x2)
300 # define ASM_ARGS_4 ASM_ARGS_3, "r" (_x3)
301 # define ASM_ARGS_5 ASM_ARGS_4, "r" (_x4)
302 # define ASM_ARGS_6 ASM_ARGS_5, "r" (_x5)
303 # define ASM_ARGS_7 ASM_ARGS_6, "r" (_x6)
305 # undef INTERNAL_SYSCALL_NCS
306 # define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
307 INTERNAL_SYSCALL_RAW (number, err, nr, args)
309 #endif /* __ASSEMBLER__ */
311 /* Pointer mangling is supported for AArch64. */
312 #if (IS_IN (rtld) || \
313 (!defined SHARED && (IS_IN (libc) \
314 || IS_IN (libpthread))))
315 # ifdef __ASSEMBLER__
316 # define PTR_MANGLE(dst, src, guard, tmp) \
317 LDST_PCREL (ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard_local)); \
318 PTR_MANGLE2 (dst, src, guard)
319 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
320 # define PTR_MANGLE2(dst, src, guard)\
321 eor dst, src, guard
322 # define PTR_DEMANGLE(dst, src, guard, tmp)\
323 PTR_MANGLE (dst, src, guard, tmp)
324 # define PTR_DEMANGLE2(dst, src, guard)\
325 PTR_MANGLE2 (dst, src, guard)
326 # else
327 extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
328 # define PTR_MANGLE(var) \
329 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard_local)
330 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
331 # endif
332 #else
333 # ifdef __ASSEMBLER__
334 # define PTR_MANGLE(dst, src, guard, tmp) \
335 LDST_GLOBAL (ldr, guard, tmp, C_SYMBOL_NAME(__pointer_chk_guard)); \
336 PTR_MANGLE2 (dst, src, guard)
337 /* Use PTR_MANGLE2 for efficiency if guard is already loaded. */
338 # define PTR_MANGLE2(dst, src, guard)\
339 eor dst, src, guard
340 # define PTR_DEMANGLE(dst, src, guard, tmp)\
341 PTR_MANGLE (dst, src, guard, tmp)
342 # define PTR_DEMANGLE2(dst, src, guard)\
343 PTR_MANGLE2 (dst, src, guard)
344 # else
345 extern uintptr_t __pointer_chk_guard attribute_relro;
346 # define PTR_MANGLE(var) \
347 (var) = (__typeof (var)) ((uintptr_t) (var) ^ __pointer_chk_guard)
348 # define PTR_DEMANGLE(var) PTR_MANGLE (var)
349 # endif
350 #endif
352 #endif /* linux/aarch64/sysdep.h */