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