]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/x86_64/sysdep.h
* rt/tst-aio7.c (do_test): Don't read from stdin, which could be the
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / x86_64 / sysdep.h
CommitLineData
ffa8d2a0 1/* Copyright (C) 2001,02 Free Software Foundation, Inc.
c9cf6dde
AJ
2 This file is part of the GNU C Library.
3
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.
8
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.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
18
19#ifndef _LINUX_X86_64_SYSDEP_H
20#define _LINUX_X86_64_SYSDEP_H 1
21
22/* There is some commonality. */
23#include <sysdeps/unix/x86_64/sysdep.h>
24#include <bp-sym.h>
25#include <bp-asm.h>
26
27/* For Linux we can use the system call table in the header file
28 /usr/include/asm/unistd.h
29 of the kernel. But these symbols do not follow the SYS_* syntax
30 so we have to redefine the `SYS_ify' macro here. */
31#undef SYS_ify
32#define SYS_ify(syscall_name) __NR_##syscall_name
33
3899afcb
RM
34/* This is a kludge to make syscalls.list find these under the names
35 pread and pwrite, since some kernel headers define those names
36 and some define the *64 names for the same system calls. */
37#if !defined __NR_pread && defined __NR_pread64
38# define __NR_pread __NR_pread64
39#endif
40#if !defined __NR_pwrite && defined __NR_pwrite64
41# define __NR_pwrite __NR_pwrite64
42#endif
43
c9cf6dde
AJ
44#ifdef __ASSEMBLER__
45
46/* Linux uses a negative return value to indicate syscall errors,
47 unlike most Unices, which use the condition codes' carry flag.
48
49 Since version 2.1 the return value of a system call might be
50 negative even if the call succeeded. E.g., the `lseek' system call
51 might return a large offset. Therefore we must not anymore test
52 for < 0, but test for a real error by making sure the value in %eax
53 is a real error number. Linus said he will make sure the no syscall
54 returns a value in -1 .. -4095 as a valid result so we can savely
55 test with -4095. */
56
57/* We don't want the label for the error handle to be global when we define
58 it here. */
59#ifdef PIC
60# define SYSCALL_ERROR_LABEL 0f
61#else
62# define SYSCALL_ERROR_LABEL syscall_error
63#endif
64
65#undef PSEUDO
66#define PSEUDO(name, syscall_name, args) \
67 .text; \
68 ENTRY (name) \
ffa8d2a0 69 DO_CALL (syscall_name, args); \
c9cf6dde
AJ
70 cmpq $-4095, %rax; \
71 jae SYSCALL_ERROR_LABEL; \
72 L(pseudo_end):
73
74#undef PSEUDO_END
75#define PSEUDO_END(name) \
76 SYSCALL_ERROR_HANDLER \
77 END (name)
78
79#ifndef PIC
80#define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
81#else
5d865001 82/* Store (- %rax) into errno through the GOT. Note that errno occupies 4 bytes. */
c9cf6dde
AJ
83#ifdef _LIBC_REENTRANT
84#define SYSCALL_ERROR_HANDLER \
850: \
86 xorq %rdx, %rdx; \
87 subq %rax, %rdx; \
88 pushq %rdx \
89 PUSH_ERRNO_LOCATION_RETURN; \
90 call BP_SYM (__errno_location)@PLT; \
91 POP_ERRNO_LOCATION_RETURN; \
92 popq %rdx; \
5d865001 93 movl %edx, (%rax); \
c9cf6dde
AJ
94 orq $-1, %rax; \
95 jmp L(pseudo_end);
96
97/* A quick note: it is assumed that the call to `__errno_location' does
98 not modify the stack! */
99#else
100#define SYSCALL_ERROR_HANDLER \
1010:movq errno@GOTPCREL(%RIP), %rcx; \
102 xorq %rdx, %rdx; \
103 subq %rax, %rdx; \
5d865001 104 movl %edx, (%rcx); \
c9cf6dde
AJ
105 orq $-1, %rax; \
106 jmp L(pseudo_end);
107#endif /* _LIBC_REENTRANT */
108#endif /* PIC */
109
110/* Linux/x86-64 takes system call arguments in registers:
111
112 Register setup:
113 system call number rax
114 arg 1 rdi
115 arg 2 rsi
116 arg 3 rdx
117 arg 4 rcx
118 arg 5 r8
119 arg 6 r9
120
121 return address from
122 syscall rcx
123 additionally clobered: r12-r15,rbx,rbp
124 eflags from syscall r11
125
126 The compiler is going to form a call by coming here, through PSEUDO, with arguments:
127
128 syscall number in the DO_CALL macro
129 arg 1 rdi
130 arg 2 rsi
131 arg 3 rdx
132 arg 4 r10
133 arg 5 r8
134 arg 6 r9
135
136 We have to take care that the stack is alignedto 16 bytes. When
137 called the stack is not aligned since the return address has just
138 been pushed.
139
140 Syscalls of more than 6 arguments are not supported. */
141
142#undef DO_CALL
ffa8d2a0 143#define DO_CALL(syscall_name, args) \
c9cf6dde
AJ
144 DOARGS_##args \
145 movq $SYS_ify (syscall_name), %rax; \
146 syscall;
147
148#define DOARGS_0 /* nothing */
149#define DOARGS_1 /* nothing */
150#define DOARGS_2 /* nothing */
151#define DOARGS_3 /* nothing */
152#define DOARGS_4 movq %rcx, %r10;
153#define DOARGS_5 DOARGS_4
154#define DOARGS_6 DOARGS_5
155
156#else /* !__ASSEMBLER__ */
157/* Define a macro which expands inline into the wrapper code for a system
158 call. */
159#undef INLINE_SYSCALL
160#define INLINE_SYSCALL(name, nr, args...) \
161 ({ \
162 unsigned long resultvar; \
163 LOAD_ARGS_##nr (args) \
164 asm volatile ( \
165 "movq %1, %%rax\n\t" \
166 "syscall\n\t" \
167 : "=a" (resultvar) \
168 : "i" (__NR_##name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
169 if (resultvar >= (unsigned long) -4095) \
170 { \
171 __set_errno (-resultvar); \
172 resultvar = (unsigned long) -1; \
173 } \
174 (long) resultvar; })
175
176#define LOAD_ARGS_0()
177#define ASM_ARGS_0
178
179#define LOAD_ARGS_1(a1) \
180 register long int _a1 asm ("rdi") = (long) (a1); \
181 LOAD_ARGS_0 ()
182#define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
183
184#define LOAD_ARGS_2(a1, a2) \
185 register long int _a2 asm ("rsi") = (long) (a2); \
186 LOAD_ARGS_1 (a1)
187#define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
188
189#define LOAD_ARGS_3(a1, a2, a3) \
190 register long int _a3 asm ("rdx") = (long) (a3); \
191 LOAD_ARGS_2 (a1, a2)
192#define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
193
194#define LOAD_ARGS_4(a1, a2, a3, a4) \
195 register long int _a4 asm ("r10") = (long) (a4); \
196 LOAD_ARGS_3 (a1, a2, a3)
197#define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
198
199#define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
200 register long int _a5 asm ("r8") = (long) (a5); \
201 LOAD_ARGS_4 (a1, a2, a3, a4)
202#define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
203
204#define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
205 register long int _a6 asm ("r9") = (long) (a6); \
206 LOAD_ARGS_5 (a1, a2, a3, a4, a5)
207#define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
208
209#endif /* __ASSEMBLER__ */
210
211#endif /* linux/x86_64/sysdep.h */