Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / unix / sysv / linux / x86_64 / sysdep.h
blobc9c4dbdc594a2bb0b963b78671b7658c8b078b32
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>
23 #include <bp-sym.h>
24 #include <bp-asm.h>
25 #include <tls.h>
27 #ifdef IS_IN_rtld
28 # include <dl-sysdep.h> /* Defines RTLD_PRIVATE_ERRNO. */
29 #endif
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. */
35 #undef SYS_ify
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
43 #endif
44 #if !defined __NR_pwrite && defined __NR_pwrite64
45 # define __NR_pwrite __NR_pwrite64
46 #endif
48 /* This is to help the old kernel headers where __NR_semtimedop is not
49 available. */
50 #ifndef __NR_semtimedop
51 # define __NR_semtimedop 220
52 #endif
55 #ifdef __ASSEMBLER__
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
66 test with -4095. */
68 /* We don't want the label for the error handle to be global when we define
69 it here. */
70 # ifdef PIC
71 # define SYSCALL_ERROR_LABEL 0f
72 # else
73 # define SYSCALL_ERROR_LABEL syscall_error
74 # endif
76 # undef PSEUDO
77 # define PSEUDO(name, syscall_name, args) \
78 .text; \
79 ENTRY (name) \
80 DO_CALL (syscall_name, args); \
81 cmpq $-4095, %rax; \
82 jae SYSCALL_ERROR_LABEL; \
83 L(pseudo_end):
85 # undef PSEUDO_END
86 # define PSEUDO_END(name) \
87 SYSCALL_ERROR_HANDLER \
88 END (name)
90 # undef PSEUDO_NOERRNO
91 # define PSEUDO_NOERRNO(name, syscall_name, args) \
92 .text; \
93 ENTRY (name) \
94 DO_CALL (syscall_name, args)
96 # undef PSEUDO_END_NOERRNO
97 # define PSEUDO_END_NOERRNO(name) \
98 END (name)
100 # define ret_NOERRNO ret
102 # undef PSEUDO_ERRVAL
103 # define PSEUDO_ERRVAL(name, syscall_name, args) \
104 .text; \
105 ENTRY (name) \
106 DO_CALL (syscall_name, args); \
107 negq %rax
109 # undef PSEUDO_END_ERRVAL
110 # define PSEUDO_END_ERRVAL(name) \
111 END (name)
113 # define ret_ERRVAL ret
115 # ifndef PIC
116 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
117 # elif RTLD_PRIVATE_ERRNO
118 # define SYSCALL_ERROR_HANDLER \
119 0: \
120 leaq rtld_errno(%rip), %rcx; \
121 xorl %edx, %edx; \
122 subq %rax, %rdx; \
123 movl %edx, (%rcx); \
124 orq $-1, %rax; \
125 jmp L(pseudo_end);
126 # else
127 # ifndef NOT_IN_libc
128 # define SYSCALL_ERROR_ERRNO __libc_errno
129 # else
130 # define SYSCALL_ERROR_ERRNO errno
131 # endif
132 # define SYSCALL_ERROR_HANDLER \
133 0: \
134 movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
135 xorl %edx, %edx; \
136 subq %rax, %rdx; \
137 movl %edx, %fs:(%rcx); \
138 orq $-1, %rax; \
139 jmp L(pseudo_end);
140 # endif /* PIC */
142 /* The Linux/x86-64 kernel expects the system call parameters in
143 registers according to the following table:
145 syscall number rax
146 arg 1 rdi
147 arg 2 rsi
148 arg 3 rdx
149 arg 4 r10
150 arg 5 r8
151 arg 6 r9
153 The Linux kernel uses and destroys internally these registers:
154 return address from
155 syscall rcx
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
164 arg 1 rdi
165 arg 2 rsi
166 arg 3 rdx
167 arg 4 rcx
168 arg 5 r8
169 arg 6 r9
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
173 been pushed.
176 Syscalls of more than 6 arguments are not supported. */
178 # undef DO_CALL
179 # define DO_CALL(syscall_name, args) \
180 DOARGS_##args \
181 movl $SYS_ify (syscall_name), %eax; \
182 syscall;
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
194 call. */
195 # undef INLINE_SYSCALL
196 # define INLINE_SYSCALL(name, nr, args...) \
197 ({ \
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...) \
210 ({ \
211 unsigned long int resultvar; \
212 LOAD_ARGS_##nr (args) \
213 LOAD_REGS_##nr \
214 asm volatile ( \
215 "syscall\n\t" \
216 : "=a" (resultvar) \
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))
230 # ifdef SHARED
231 # define INLINE_VSYSCALL(name, nr, args...) \
232 ({ \
233 __label__ out; \
234 __label__ iserr; \
235 INTERNAL_SYSCALL_DECL (sc_err); \
236 long int sc_ret; \
238 __typeof (__vdso_##name) vdsop = __vdso_##name; \
239 PTR_DEMANGLE (vdsop); \
240 if (vdsop != NULL) \
242 sc_ret = vdsop (args); \
243 if (!INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
244 goto out; \
245 if (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err) != ENOSYS) \
246 goto iserr; \
249 sc_ret = INTERNAL_SYSCALL (name, sc_err, nr, ##args); \
250 if (INTERNAL_SYSCALL_ERROR_P (sc_ret, sc_err)) \
252 iserr: \
253 __set_errno (INTERNAL_SYSCALL_ERRNO (sc_ret, sc_err)); \
254 sc_ret = -1L; \
256 out: \
257 sc_ret; \
259 # define INTERNAL_VSYSCALL(name, err, nr, args...) \
260 ({ \
261 __label__ out; \
262 long int v_ret; \
264 __typeof (__vdso_##name) vdsop = __vdso_##name; \
265 PTR_DEMANGLE (vdsop); \
266 if (vdsop != NULL) \
268 v_ret = vdsop (args); \
269 if (!INTERNAL_SYSCALL_ERROR_P (v_ret, err) \
270 || INTERNAL_SYSCALL_ERRNO (v_ret, err) != ENOSYS) \
271 goto out; \
273 v_ret = INTERNAL_SYSCALL (name, err, nr, ##args); \
274 out: \
275 v_ret; \
278 # else
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)
283 # endif
285 # define LOAD_ARGS_0()
286 # define LOAD_REGS_0
287 # define ASM_ARGS_0
289 # define LOAD_ARGS_1(a1) \
290 long int __arg1 = (long int) (a1); \
291 LOAD_ARGS_0 ()
292 # define LOAD_REGS_1 \
293 register long int _a1 asm ("rdi") = __arg1; \
294 LOAD_REGS_0
295 # define ASM_ARGS_1 ASM_ARGS_0, "r" (_a1)
297 # define LOAD_ARGS_2(a1, a2) \
298 long int __arg2 = (long int) (a2); \
299 LOAD_ARGS_1 (a1)
300 # define LOAD_REGS_2 \
301 register long int _a2 asm ("rsi") = __arg2; \
302 LOAD_REGS_1
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); \
307 LOAD_ARGS_2 (a1, a2)
308 # define LOAD_REGS_3 \
309 register long int _a3 asm ("rdx") = __arg3; \
310 LOAD_REGS_2
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; \
318 LOAD_REGS_3
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; \
326 LOAD_REGS_4
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; \
334 LOAD_REGS_5
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; \
346 rolq $17, reg
347 # define PTR_DEMANGLE(reg) rorq $17, reg; \
348 xorq __pointer_chk_guard_local(%rip), reg
349 # else
350 # define PTR_MANGLE(reg) asm ("xorq __pointer_chk_guard_local(%%rip), %0\n" \
351 "rolq $17, %0" \
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))
356 # endif
357 #else
358 # ifdef __ASSEMBLER__
359 # define PTR_MANGLE(reg) xorq %fs:POINTER_GUARD, reg; \
360 rolq $17, reg
361 # define PTR_DEMANGLE(reg) rorq $17, reg; \
362 xorq %fs:POINTER_GUARD, reg
363 # else
364 # define PTR_MANGLE(var) asm ("xorq %%fs:%c2, %0\n" \
365 "rolq $17, %0" \
366 : "=r" (var) \
367 : "0" (var), \
368 "i" (offsetof (tcbhead_t, \
369 pointer_guard)))
370 # define PTR_DEMANGLE(var) asm ("rorq $17, %0\n" \
371 "xorq %%fs:%c2, %0" \
372 : "=r" (var) \
373 : "0" (var), \
374 "i" (offsetof (tcbhead_t, \
375 pointer_guard)))
376 # endif
377 #endif
379 #endif /* linux/x86_64/sysdep.h */