1 /* Copyright (C) 2001-2005, 2007, 2011 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #ifndef _LINUX_X86_64_SYSDEP_H
19 #define _LINUX_X86_64_SYSDEP_H 1
21 /* There is some commonality. */
22 #include <sysdeps/unix/x86_64/sysdep.h>
28 # include <dl-sysdep.h> /* Defines RTLD_PRIVATE_ERRNO. */
31 /* For Linux we can use the system call table in the header file
32 /usr/include/asm/unistd.h
33 of the kernel. But these symbols do not follow the SYS_* syntax
34 so we have to redefine the `SYS_ify' macro here. */
36 #define SYS_ify(syscall_name) __NR_##syscall_name
38 /* This is a kludge to make syscalls.list find these under the names
39 pread and pwrite, since some kernel headers define those names
40 and some define the *64 names for the same system calls. */
41 #if !defined __NR_pread && defined __NR_pread64
42 # define __NR_pread __NR_pread64
44 #if !defined __NR_pwrite && defined __NR_pwrite64
45 # define __NR_pwrite __NR_pwrite64
48 /* This is to help the old kernel headers where __NR_semtimedop is not
50 #ifndef __NR_semtimedop
51 # define __NR_semtimedop 220
57 /* Linux uses a negative return value to indicate syscall errors,
58 unlike most Unices, which use the condition codes' carry flag.
60 Since version 2.1 the return value of a system call might be
61 negative even if the call succeeded. E.g., the `lseek' system call
62 might return a large offset. Therefore we must not anymore test
63 for < 0, but test for a real error by making sure the value in %eax
64 is a real error number. Linus said he will make sure the no syscall
65 returns a value in -1 .. -4095 as a valid result so we can savely
68 /* We don't want the label for the error handle to be global when we define
71 # define SYSCALL_ERROR_LABEL 0f
73 # define SYSCALL_ERROR_LABEL syscall_error
77 # define PSEUDO(name, syscall_name, args) \
80 DO_CALL (syscall_name, args); \
82 jae SYSCALL_ERROR_LABEL; \
86 # define PSEUDO_END(name) \
87 SYSCALL_ERROR_HANDLER \
90 # undef PSEUDO_NOERRNO
91 # define PSEUDO_NOERRNO(name, syscall_name, args) \
94 DO_CALL (syscall_name, args)
96 # undef PSEUDO_END_NOERRNO
97 # define PSEUDO_END_NOERRNO(name) \
100 # define ret_NOERRNO ret
102 # undef PSEUDO_ERRVAL
103 # define PSEUDO_ERRVAL(name, syscall_name, args) \
106 DO_CALL (syscall_name, args); \
109 # undef PSEUDO_END_ERRVAL
110 # define PSEUDO_END_ERRVAL(name) \
113 # define ret_ERRVAL ret
116 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
117 # elif RTLD_PRIVATE_ERRNO
118 # define SYSCALL_ERROR_HANDLER \
120 leaq rtld_errno(%rip), %rcx; \
128 # define SYSCALL_ERROR_ERRNO __libc_errno
130 # define SYSCALL_ERROR_ERRNO errno
132 # define SYSCALL_ERROR_HANDLER \
134 movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
137 movl %edx, %fs:(%rcx); \
142 /* The Linux/x86-64 kernel expects the system call parameters in
143 registers according to the following table:
153 The Linux kernel uses and destroys internally these registers:
156 eflags from syscall r11
158 Normal function call, including calls to the system call stub
159 functions in the libc, get the first six parameters passed in
160 registers and the seventh parameter and later on the stack. The
161 register use is as follows:
163 system call number in the DO_CALL macro
171 We have to take care that the stack is aligned to 16 bytes. When
172 called the stack is not aligned since the return address has just
176 Syscalls of more than 6 arguments are not supported. */
179 # define DO_CALL(syscall_name, args) \
181 movl $SYS_ify (syscall_name), %eax; \
184 # define DOARGS_0 /* nothing */
185 # define DOARGS_1 /* nothing */
186 # define DOARGS_2 /* nothing */
187 # define DOARGS_3 /* nothing */
188 # define DOARGS_4 movq %rcx, %r10;
189 # define DOARGS_5 DOARGS_4
190 # define DOARGS_6 DOARGS_5
192 #else /* !__ASSEMBLER__ */
193 /* Define a macro which expands inline into the wrapper code for a system
195 # undef INLINE_SYSCALL
196 # define INLINE_SYSCALL(name, nr, args...) \
198 unsigned long int resultvar = INTERNAL_SYSCALL (name, , nr, args); \
199 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (resultvar, ), 0)) \
201 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
202 resultvar = (unsigned long int) -1; \
204 (long int) resultvar; })
206 # undef INTERNAL_SYSCALL_DECL
207 # define INTERNAL_SYSCALL_DECL(err) do { } while (0)
209 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
211 unsigned long int resultvar; \
212 LOAD_ARGS_##nr (args) \
217 : "0" (name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
218 (long int) resultvar; })
219 # undef INTERNAL_SYSCALL
220 # define INTERNAL_SYSCALL(name, err, nr, args...) \
221 INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
223 # undef INTERNAL_SYSCALL_ERROR_P
224 # define INTERNAL_SYSCALL_ERROR_P(val, err) \
225 ((unsigned long int) (long int) (val) >= -4095L)
227 # undef INTERNAL_SYSCALL_ERRNO
228 # define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
231 # define INLINE_VSYSCALL(name, nr, args...) \
235 INTERNAL_SYSCALL_DECL (sc_err); \
238 __typeof (__vdso_##name) vdsop = __vdso_##name; \
239 PTR_DEMANGLE (vdsop); \
242 sc_ret = vdsop (args); \
243 if (!INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
245 if (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err) != ENOSYS) \
249 sc_ret = INTERNAL_SYSCALL (name, sc_err, nr, ##args); \
250 if (INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
253 __set_errno (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err)); \
259 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
264 __typeof (__vdso_##name) vdsop = __vdso_##name; \
265 PTR_DEMANGLE (vdsop); \
268 v_ret = vdsop (args); \
269 if (!INTERNAL_SYSCALL_ERROR_P (v_ret, err) \
270 || INTERNAL_SYSCALL_ERRNO (v_ret, err) != ENOSYS) \
273 v_ret = INTERNAL_SYSCALL (name, err, nr, ##args); \
279 # define INLINE_VSYSCALL(name, nr, args...) \
280 INLINE_SYSCALL (name, nr, ##args)
281 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
282 INTERNAL_SYSCALL (name, err, nr, ##args)
285 # define LOAD_ARGS_0()
289 # define LOAD_ARGS_1(a1) \
290 long int __arg1 = (long int) (a1); \
292 # define LOAD_REGS_1 \
293 register long int _a1 asm ("rdi") = __arg1; \
295 # define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
297 # define LOAD_ARGS_2(a1, a2) \
298 long int __arg2 = (long int) (a2); \
300 # define LOAD_REGS_2 \
301 register long int _a2 asm ("rsi") = __arg2; \
303 # define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
305 # define LOAD_ARGS_3(a1, a2, a3) \
306 long int __arg3 = (long int) (a3); \
308 # define LOAD_REGS_3 \
309 register long int _a3 asm ("rdx") = __arg3; \
311 # define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
313 # define LOAD_ARGS_4(a1, a2, a3, a4) \
314 long int __arg4 = (long int) (a4); \
315 LOAD_ARGS_3 (a1, a2, a3)
316 # define LOAD_REGS_4 \
317 register long int _a4 asm ("r10") = __arg4; \
319 # define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
321 # define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
322 long int __arg5 = (long int) (a5); \
323 LOAD_ARGS_4 (a1, a2, a3, a4)
324 # define LOAD_REGS_5 \
325 register long int _a5 asm ("r8") = __arg5; \
327 # define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
329 # define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
330 long int __arg6 = (long int) (a6); \
331 LOAD_ARGS_5 (a1, a2, a3, a4, a5)
332 # define LOAD_REGS_6 \
333 register long int _a6 asm ("r9") = __arg6; \
335 # define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
337 #endif /* __ASSEMBLER__ */
340 /* Pointer mangling support. */
341 #if defined NOT_IN_libc && defined IS_IN_rtld
342 /* We cannot use the thread descriptor because in ld.so we use setjmp
343 earlier than the descriptor is initialized. */
344 # ifdef __ASSEMBLER__
345 # define PTR_MANGLE(reg) xorq __pointer_chk_guard_local(%rip), reg; \
347 # define PTR_DEMANGLE(reg) rorq $17, reg; \
348 xorq __pointer_chk_guard_local(%rip), reg
350 # define PTR_MANGLE(reg) asm ("xorq __pointer_chk_guard_local(%%rip), %0\n" \
352 : "=r" (reg) : "0" (reg))
353 # define PTR_DEMANGLE(reg) asm ("rorq $17, %0\n" \
354 "xorq __pointer_chk_guard_local(%%rip), %0" \
355 : "=r" (reg) : "0" (reg))
358 # ifdef __ASSEMBLER__
359 # define PTR_MANGLE(reg) xorq %fs:POINTER_GUARD, reg; \
361 # define PTR_DEMANGLE(reg) rorq $17, reg; \
362 xorq %fs:POINTER_GUARD, reg
364 # define PTR_MANGLE(var) asm ("xorq %%fs:%c2, %0\n" \
368 "i" (offsetof (tcbhead_t, \
370 # define PTR_DEMANGLE(var) asm ("rorq $17, %0\n" \
371 "xorq %%fs:%c2, %0" \
374 "i" (offsetof (tcbhead_t, \
379 #endif /* linux/x86_64/sysdep.h */