1 /* Copyright (C) 2001-2015 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>
26 # include <dl-sysdep.h> /* Defines RTLD_PRIVATE_ERRNO. */
29 /* For Linux we can use the system call table in the header file
30 /usr/include/asm/unistd.h
31 of the kernel. But these symbols do not follow the SYS_* syntax
32 so we have to redefine the `SYS_ify' macro here. */
34 #define SYS_ify(syscall_name) __NR_##syscall_name
36 /* This is a kludge to make syscalls.list find these under the names
37 pread and pwrite, since some kernel headers define those names
38 and some define the *64 names for the same system calls. */
39 #if !defined __NR_pread && defined __NR_pread64
40 # define __NR_pread __NR_pread64
42 #if !defined __NR_pwrite && defined __NR_pwrite64
43 # define __NR_pwrite __NR_pwrite64
46 /* This is to help the old kernel headers where __NR_semtimedop is not
48 #ifndef __NR_semtimedop
49 # define __NR_semtimedop 220
55 /* Linux uses a negative return value to indicate syscall errors,
56 unlike most Unices, which use the condition codes' carry flag.
58 Since version 2.1 the return value of a system call might be
59 negative even if the call succeeded. E.g., the `lseek' system call
60 might return a large offset. Therefore we must not anymore test
61 for < 0, but test for a real error by making sure the value in %eax
62 is a real error number. Linus said he will make sure the no syscall
63 returns a value in -1 .. -4095 as a valid result so we can savely
66 /* We don't want the label for the error handle to be global when we define
69 # define SYSCALL_ERROR_LABEL 0f
71 # define SYSCALL_ERROR_LABEL syscall_error
75 # define PSEUDO(name, syscall_name, args) \
78 DO_CALL (syscall_name, args); \
80 jae SYSCALL_ERROR_LABEL
83 # define PSEUDO_END(name) \
84 SYSCALL_ERROR_HANDLER \
87 # undef PSEUDO_NOERRNO
88 # define PSEUDO_NOERRNO(name, syscall_name, args) \
91 DO_CALL (syscall_name, args)
93 # undef PSEUDO_END_NOERRNO
94 # define PSEUDO_END_NOERRNO(name) \
97 # define ret_NOERRNO ret
100 # define PSEUDO_ERRVAL(name, syscall_name, args) \
103 DO_CALL (syscall_name, args); \
106 # undef PSEUDO_END_ERRVAL
107 # define PSEUDO_END_ERRVAL(name) \
110 # define ret_ERRVAL ret
112 # if defined PIC && defined RTLD_PRIVATE_ERRNO
113 # define SYSCALL_SET_ERRNO \
114 lea rtld_errno(%rip), %RCX_LP; \
119 # define SYSCALL_ERROR_ERRNO __libc_errno
121 # define SYSCALL_ERROR_ERRNO errno
123 # define SYSCALL_SET_ERRNO \
124 movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
126 movl %eax, %fs:(%rcx);
130 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
132 # define SYSCALL_ERROR_HANDLER \
139 /* The Linux/x86-64 kernel expects the system call parameters in
140 registers according to the following table:
150 The Linux kernel uses and destroys internally these registers:
153 eflags from syscall r11
155 Normal function call, including calls to the system call stub
156 functions in the libc, get the first six parameters passed in
157 registers and the seventh parameter and later on the stack. The
158 register use is as follows:
160 system call number in the DO_CALL macro
168 We have to take care that the stack is aligned to 16 bytes. When
169 called the stack is not aligned since the return address has just
173 Syscalls of more than 6 arguments are not supported. */
176 # define DO_CALL(syscall_name, args) \
178 movl $SYS_ify (syscall_name), %eax; \
181 # define DOARGS_0 /* nothing */
182 # define DOARGS_1 /* nothing */
183 # define DOARGS_2 /* nothing */
184 # define DOARGS_3 /* nothing */
185 # define DOARGS_4 movq %rcx, %r10;
186 # define DOARGS_5 DOARGS_4
187 # define DOARGS_6 DOARGS_5
189 #else /* !__ASSEMBLER__ */
190 /* Define a macro which expands inline into the wrapper code for a system
192 # undef INLINE_SYSCALL
193 # define INLINE_SYSCALL(name, nr, args...) \
195 unsigned long int resultvar = INTERNAL_SYSCALL (name, , nr, args); \
196 if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (resultvar, ))) \
198 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
199 resultvar = (unsigned long int) -1; \
201 (long int) resultvar; })
203 /* Define a macro with explicit types for arguments, which expands inline
204 into the wrapper code for a system call. It should be used when size
205 of any argument > size of long int. */
206 # undef INLINE_SYSCALL_TYPES
207 # define INLINE_SYSCALL_TYPES(name, nr, args...) \
209 unsigned long int resultvar = INTERNAL_SYSCALL_TYPES (name, , nr, args); \
210 if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (resultvar, ))) \
212 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
213 resultvar = (unsigned long int) -1; \
215 (long int) resultvar; })
217 # undef INTERNAL_SYSCALL_DECL
218 # define INTERNAL_SYSCALL_DECL(err) do { } while (0)
220 # define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
222 unsigned long int resultvar; \
223 LOAD_ARGS_##nr (args) \
228 : "0" (name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
229 (long int) resultvar; })
230 # undef INTERNAL_SYSCALL
231 # define INTERNAL_SYSCALL(name, err, nr, args...) \
232 INTERNAL_SYSCALL_NCS (__NR_##name, err, nr, ##args)
234 # define INTERNAL_SYSCALL_NCS_TYPES(name, err, nr, args...) \
236 unsigned long int resultvar; \
237 LOAD_ARGS_TYPES_##nr (args) \
238 LOAD_REGS_TYPES_##nr (args) \
242 : "0" (name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
243 (long int) resultvar; })
244 # undef INTERNAL_SYSCALL_TYPES
245 # define INTERNAL_SYSCALL_TYPES(name, err, nr, args...) \
246 INTERNAL_SYSCALL_NCS_TYPES (__NR_##name, err, nr, ##args)
248 # undef INTERNAL_SYSCALL_ERROR_P
249 # define INTERNAL_SYSCALL_ERROR_P(val, err) \
250 ((unsigned long int) (long int) (val) >= -4095L)
252 # undef INTERNAL_SYSCALL_ERRNO
253 # define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
256 # define INLINE_VSYSCALL(name, nr, args...) \
260 INTERNAL_SYSCALL_DECL (sc_err); \
263 __typeof (__vdso_##name) vdsop = __vdso_##name; \
264 PTR_DEMANGLE (vdsop); \
267 sc_ret = vdsop (args); \
268 if (!INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
270 if (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err) != ENOSYS) \
274 sc_ret = INTERNAL_SYSCALL (name, sc_err, nr, ##args); \
275 if (INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
278 __set_errno (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err)); \
284 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
289 __typeof (__vdso_##name) vdsop = __vdso_##name; \
290 PTR_DEMANGLE (vdsop); \
293 v_ret = vdsop (args); \
294 if (!INTERNAL_SYSCALL_ERROR_P (v_ret, err) \
295 || INTERNAL_SYSCALL_ERRNO (v_ret, err) != ENOSYS) \
298 v_ret = INTERNAL_SYSCALL (name, err, nr, ##args); \
304 # define INLINE_VSYSCALL(name, nr, args...) \
305 INLINE_SYSCALL (name, nr, ##args)
306 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
307 INTERNAL_SYSCALL (name, err, nr, ##args)
310 # define LOAD_ARGS_0()
314 # define LOAD_ARGS_TYPES_1(t1, a1) \
315 t1 __arg1 = (t1) (a1); \
317 # define LOAD_REGS_TYPES_1(t1, a1) \
318 register t1 _a1 asm ("rdi") = __arg1; \
320 # define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
321 # define LOAD_ARGS_1(a1) \
322 LOAD_ARGS_TYPES_1 (long int, a1)
323 # define LOAD_REGS_1 \
324 LOAD_REGS_TYPES_1 (long int, a1)
326 # define LOAD_ARGS_TYPES_2(t1, a1, t2, a2) \
327 t2 __arg2 = (t2) (a2); \
328 LOAD_ARGS_TYPES_1 (t1, a1)
329 # define LOAD_REGS_TYPES_2(t1, a1, t2, a2) \
330 register t2 _a2 asm ("rsi") = __arg2; \
331 LOAD_REGS_TYPES_1(t1, a1)
332 # define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
333 # define LOAD_ARGS_2(a1, a2) \
334 LOAD_ARGS_TYPES_2 (long int, a1, long int, a2)
335 # define LOAD_REGS_2 \
336 LOAD_REGS_TYPES_2 (long int, a1, long int, a2)
338 # define LOAD_ARGS_TYPES_3(t1, a1, t2, a2, t3, a3) \
339 t3 __arg3 = (t3) (a3); \
340 LOAD_ARGS_TYPES_2 (t1, a1, t2, a2)
341 # define LOAD_REGS_TYPES_3(t1, a1, t2, a2, t3, a3) \
342 register t3 _a3 asm ("rdx") = __arg3; \
343 LOAD_REGS_TYPES_2(t1, a1, t2, a2)
344 # define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
345 # define LOAD_ARGS_3(a1, a2, a3) \
346 LOAD_ARGS_TYPES_3 (long int, a1, long int, a2, long int, a3)
347 # define LOAD_REGS_3 \
348 LOAD_REGS_TYPES_3 (long int, a1, long int, a2, long int, a3)
350 # define LOAD_ARGS_TYPES_4(t1, a1, t2, a2, t3, a3, t4, a4) \
351 t4 __arg4 = (t4) (a4); \
352 LOAD_ARGS_TYPES_3 (t1, a1, t2, a2, t3, a3)
353 # define LOAD_REGS_TYPES_4(t1, a1, t2, a2, t3, a3, t4, a4) \
354 register t4 _a4 asm ("r10") = __arg4; \
355 LOAD_REGS_TYPES_3(t1, a2, t2, a2, t3, a3)
356 # define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
357 # define LOAD_ARGS_4(a1, a2, a3, a4) \
358 LOAD_ARGS_TYPES_4 (long int, a1, long int, a2, long int, a3, \
360 # define LOAD_REGS_4 \
361 LOAD_REGS_TYPES_4 (long int, a1, long int, a2, long int, a3, \
364 # define LOAD_ARGS_TYPES_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \
365 t5 __arg5 = (t5) (a5); \
366 LOAD_ARGS_TYPES_4 (t1, a1, t2, a2, t3, a3, t4, a4)
367 # define LOAD_REGS_TYPES_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \
368 register t5 _a5 asm ("r8") = __arg5; \
369 LOAD_REGS_TYPES_4 (t1, a1, t2, a2, t3, a3, t4, a4)
370 # define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
371 # define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
372 LOAD_ARGS_TYPES_5 (long int, a1, long int, a2, long int, a3, \
373 long int, a4, long int, a5)
374 # define LOAD_REGS_5 \
375 LOAD_REGS_TYPES_5 (long int, a1, long int, a2, long int, a3, \
376 long int, a4, long int, a5)
378 # define LOAD_ARGS_TYPES_6(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5, t6, a6) \
379 t6 __arg6 = (t6) (a6); \
380 LOAD_ARGS_TYPES_5 (t1, a1, t2, a2, t3, a3, t4, a4, t5, a5)
381 # define LOAD_REGS_TYPES_6(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5, t6, a6) \
382 register t6 _a6 asm ("r9") = __arg6; \
383 LOAD_REGS_TYPES_5 (t1, a1, t2, a2, t3, a3, t4, a4, t5, a5)
384 # define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
385 # define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
386 LOAD_ARGS_TYPES_6 (long int, a1, long int, a2, long int, a3, \
387 long int, a4, long int, a5, long int, a6)
388 # define LOAD_REGS_6 \
389 LOAD_REGS_TYPES_6 (long int, a1, long int, a2, long int, a3, \
390 long int, a4, long int, a5, long int, a6)
392 #endif /* __ASSEMBLER__ */
395 /* Pointer mangling support. */
397 /* We cannot use the thread descriptor because in ld.so we use setjmp
398 earlier than the descriptor is initialized. */
399 # ifdef __ASSEMBLER__
400 # define PTR_MANGLE(reg) xor __pointer_chk_guard_local(%rip), reg; \
401 rol $2*LP_SIZE+1, reg
402 # define PTR_DEMANGLE(reg) ror $2*LP_SIZE+1, reg; \
403 xor __pointer_chk_guard_local(%rip), reg
405 # define PTR_MANGLE(reg) asm ("xor __pointer_chk_guard_local(%%rip), %0\n" \
406 "rol $2*" LP_SIZE "+1, %0" \
407 : "=r" (reg) : "0" (reg))
408 # define PTR_DEMANGLE(reg) asm ("ror $2*" LP_SIZE "+1, %0\n" \
409 "xor __pointer_chk_guard_local(%%rip), %0" \
410 : "=r" (reg) : "0" (reg))
413 # ifdef __ASSEMBLER__
414 # define PTR_MANGLE(reg) xor %fs:POINTER_GUARD, reg; \
415 rol $2*LP_SIZE+1, reg
416 # define PTR_DEMANGLE(reg) ror $2*LP_SIZE+1, reg; \
417 xor %fs:POINTER_GUARD, reg
419 # define PTR_MANGLE(var) asm ("xor %%fs:%c2, %0\n" \
420 "rol $2*" LP_SIZE "+1, %0" \
423 "i" (offsetof (tcbhead_t, \
425 # define PTR_DEMANGLE(var) asm ("ror $2*" LP_SIZE "+1, %0\n" \
429 "i" (offsetof (tcbhead_t, \
434 #endif /* linux/x86_64/sysdep.h */