]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/x86_64/sysdep.h
Clarify comment.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / x86_64 / sysdep.h
1 /* Copyright (C) 2001,02,03 Free Software Foundation, Inc.
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 #include <tls.h>
27
28 #ifdef IS_IN_rtld
29 # include <dl-sysdep.h> /* Defines RTLD_PRIVATE_ERRNO. */
30 #endif
31
32 /* For Linux we can use the system call table in the header file
33 /usr/include/asm/unistd.h
34 of the kernel. But these symbols do not follow the SYS_* syntax
35 so we have to redefine the `SYS_ify' macro here. */
36 #undef SYS_ify
37 #define SYS_ify(syscall_name) __NR_##syscall_name
38
39 /* This is a kludge to make syscalls.list find these under the names
40 pread and pwrite, since some kernel headers define those names
41 and some define the *64 names for the same system calls. */
42 #if !defined __NR_pread && defined __NR_pread64
43 # define __NR_pread __NR_pread64
44 #endif
45 #if !defined __NR_pwrite && defined __NR_pwrite64
46 # define __NR_pwrite __NR_pwrite64
47 #endif
48
49 #ifdef __ASSEMBLER__
50
51 /* Linux uses a negative return value to indicate syscall errors,
52 unlike most Unices, which use the condition codes' carry flag.
53
54 Since version 2.1 the return value of a system call might be
55 negative even if the call succeeded. E.g., the `lseek' system call
56 might return a large offset. Therefore we must not anymore test
57 for < 0, but test for a real error by making sure the value in %eax
58 is a real error number. Linus said he will make sure the no syscall
59 returns a value in -1 .. -4095 as a valid result so we can savely
60 test with -4095. */
61
62 /* We don't want the label for the error handle to be global when we define
63 it here. */
64 #ifdef PIC
65 # define SYSCALL_ERROR_LABEL 0f
66 #else
67 # define SYSCALL_ERROR_LABEL syscall_error
68 #endif
69
70 #undef PSEUDO
71 #define PSEUDO(name, syscall_name, args) \
72 .text; \
73 ENTRY (name) \
74 DO_CALL (syscall_name, args); \
75 cmpq $-4095, %rax; \
76 jae SYSCALL_ERROR_LABEL; \
77 L(pseudo_end):
78
79 #undef PSEUDO_END
80 #define PSEUDO_END(name) \
81 SYSCALL_ERROR_HANDLER \
82 END (name)
83
84 #ifndef PIC
85 #define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
86 #elif RTLD_PRIVATE_ERRNO
87 # define SYSCALL_ERROR_HANDLER \
88 0: \
89 leaq errno(%rip), %rcx; \
90 xorq %rdx, %rdx; \
91 subq %rax, %rdx; \
92 movl %edx, (%rcx); \
93 orq $-1, %rax; \
94 jmp L(pseudo_end);
95 #elif USE___THREAD
96 # ifndef NOT_IN_libc
97 # define SYSCALL_ERROR_ERRNO __libc_errno
98 # else
99 # define SYSCALL_ERROR_ERRNO errno
100 # endif
101 # define SYSCALL_ERROR_HANDLER \
102 0: \
103 movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
104 xorq %rdx, %rdx; \
105 subq %rax, %rdx; \
106 movl %edx, %fs:(%rcx); \
107 orq $-1, %rax; \
108 jmp L(pseudo_end);
109 #elif defined _LIBC_REENTRANT
110 /* Store (- %rax) into errno through the GOT.
111 Note that errno occupies only 4 bytes. */
112 # define SYSCALL_ERROR_HANDLER \
113 0: \
114 xorq %rdx, %rdx; \
115 subq %rax, %rdx; \
116 pushq %rdx \
117 PUSH_ERRNO_LOCATION_RETURN; \
118 call BP_SYM (__errno_location)@PLT; \
119 POP_ERRNO_LOCATION_RETURN; \
120 popq %rdx; \
121 movl %edx, (%rax); \
122 orq $-1, %rax; \
123 jmp L(pseudo_end);
124
125 /* A quick note: it is assumed that the call to `__errno_location' does
126 not modify the stack! */
127 #else /* Not _LIBC_REENTRANT. */
128 # define SYSCALL_ERROR_HANDLER \
129 0:movq errno@GOTPCREL(%RIP), %rcx; \
130 xorq %rdx, %rdx; \
131 subq %rax, %rdx; \
132 movl %edx, (%rcx); \
133 orq $-1, %rax; \
134 jmp L(pseudo_end);
135 #endif /* PIC */
136
137 /* The Linux/x86-64 kernel expects the system call parameters in
138 registers according to the following table:
139
140 syscall number rax
141 arg 1 rdi
142 arg 2 rsi
143 arg 3 rdx
144 arg 4 r10
145 arg 5 r8
146 arg 6 r9
147
148 The Linux kernel uses and destroys internally these registers:
149 return address from
150 syscall rcx
151 additionally clobered: r12-r15,rbx,rbp
152 eflags from syscall r11
153
154 Normal function call, including calls to the system call stub
155 functions in the libc, get the first six parameters passed in
156 registers and the seventh parameter and later on the stack. The
157 register use is as follows:
158
159 system call number in the DO_CALL macro
160 arg 1 rdi
161 arg 2 rsi
162 arg 3 rdx
163 arg 4 rcx
164 arg 5 r8
165 arg 6 r9
166
167 We have to take care that the stack is aligned to 16 bytes. When
168 called the stack is not aligned since the return address has just
169 been pushed.
170
171
172 Syscalls of more than 6 arguments are not supported. */
173
174 #undef DO_CALL
175 #define DO_CALL(syscall_name, args) \
176 DOARGS_##args \
177 movq $SYS_ify (syscall_name), %rax; \
178 syscall;
179
180 #define DOARGS_0 /* nothing */
181 #define DOARGS_1 /* nothing */
182 #define DOARGS_2 /* nothing */
183 #define DOARGS_3 /* nothing */
184 #define DOARGS_4 movq %rcx, %r10;
185 #define DOARGS_5 DOARGS_4
186 #define DOARGS_6 DOARGS_5
187
188 #else /* !__ASSEMBLER__ */
189 /* Define a macro which expands inline into the wrapper code for a system
190 call. */
191 #undef INLINE_SYSCALL
192 #define INLINE_SYSCALL(name, nr, args...) \
193 ({ \
194 unsigned long resultvar = INTERNAL_SYSCALL (name, , nr, args); \
195 if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (resultvar, ), 0)) \
196 { \
197 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
198 resultvar = (unsigned long) -1; \
199 } \
200 (long) resultvar; })
201
202 #undef INTERNAL_SYSCALL_DECL
203 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
204
205 #undef INTERNAL_SYSCALL
206 #define INTERNAL_SYSCALL(name, err, nr, args...) \
207 ({ \
208 unsigned long resultvar; \
209 LOAD_ARGS_##nr (args) \
210 asm volatile ( \
211 "movq %1, %%rax\n\t" \
212 "syscall\n\t" \
213 : "=a" (resultvar) \
214 : "i" (__NR_##name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx"); \
215 (long) resultvar; })
216
217 #undef INTERNAL_SYSCALL_ERROR_P
218 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
219 ((unsigned long) (val) >= -4095L)
220
221 #undef INTERNAL_SYSCALL_ERRNO
222 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
223
224 #define LOAD_ARGS_0()
225 #define ASM_ARGS_0
226
227 #define LOAD_ARGS_1(a1) \
228 register long int _a1 asm ("rdi") = (long) (a1); \
229 LOAD_ARGS_0 ()
230 #define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
231
232 #define LOAD_ARGS_2(a1, a2) \
233 register long int _a2 asm ("rsi") = (long) (a2); \
234 LOAD_ARGS_1 (a1)
235 #define ASM_ARGS_2 ASM_ARGS_1, "r" (_a2)
236
237 #define LOAD_ARGS_3(a1, a2, a3) \
238 register long int _a3 asm ("rdx") = (long) (a3); \
239 LOAD_ARGS_2 (a1, a2)
240 #define ASM_ARGS_3 ASM_ARGS_2, "r" (_a3)
241
242 #define LOAD_ARGS_4(a1, a2, a3, a4) \
243 register long int _a4 asm ("r10") = (long) (a4); \
244 LOAD_ARGS_3 (a1, a2, a3)
245 #define ASM_ARGS_4 ASM_ARGS_3, "r" (_a4)
246
247 #define LOAD_ARGS_5(a1, a2, a3, a4, a5) \
248 register long int _a5 asm ("r8") = (long) (a5); \
249 LOAD_ARGS_4 (a1, a2, a3, a4)
250 #define ASM_ARGS_5 ASM_ARGS_4, "r" (_a5)
251
252 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6) \
253 register long int _a6 asm ("r9") = (long) (a6); \
254 LOAD_ARGS_5 (a1, a2, a3, a4, a5)
255 #define ASM_ARGS_6 ASM_ARGS_5, "r" (_a6)
256
257 #endif /* __ASSEMBLER__ */
258
259 #endif /* linux/x86_64/sysdep.h */