benchtests: send non-consumable data to stderr
[glibc.git] / sysdeps / unix / sysv / linux / x86_64 / sysdep.h
blob9f49347ce5d1bff5f9af2c465d06c20031204f6f
1 /* Copyright (C) 2001-2018 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 /* Always enable vsyscalls on x86_64 */
22 #define ALWAYS_USE_VSYSCALL 1
24 /* There is some commonality. */
25 #include <sysdeps/unix/sysv/linux/sysdep.h>
26 #include <sysdeps/unix/x86_64/sysdep.h>
27 #include <tls.h>
29 /* Defines RTLD_PRIVATE_ERRNO. */
30 #include <dl-sysdep.h>
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
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
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
56 #ifdef __ASSEMBLER__
58 /* Linux uses a negative return value to indicate syscall errors,
59 unlike most Unices, which use the condition codes' carry flag.
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. */
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
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
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 # if defined PIC && RTLD_PRIVATE_ERRNO
116 # define SYSCALL_SET_ERRNO \
117 lea rtld_errno(%rip), %RCX_LP; \
118 neg %eax; \
119 movl %eax, (%rcx)
120 # else
121 # if IS_IN (libc)
122 # define SYSCALL_ERROR_ERRNO __libc_errno
123 # else
124 # define SYSCALL_ERROR_ERRNO errno
125 # endif
126 # define SYSCALL_SET_ERRNO \
127 movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
128 neg %eax; \
129 movl %eax, %fs:(%rcx);
130 # endif
132 # ifndef PIC
133 # define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
134 # else
135 # define SYSCALL_ERROR_HANDLER \
136 0: \
137 SYSCALL_SET_ERRNO; \
138 or $-1, %RAX_LP; \
139 ret;
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 (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (resultvar, ))) \
201 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
202 resultvar = (unsigned long int) -1; \
204 (long int) resultvar; })
206 /* Define a macro with explicit types for arguments, which expands inline
207 into the wrapper code for a system call. It should be used when size
208 of any argument > size of long int. */
209 # undef INLINE_SYSCALL_TYPES
210 # define INLINE_SYSCALL_TYPES(name, nr, args...) \
211 ({ \
212 unsigned long int resultvar = INTERNAL_SYSCALL_TYPES (name, , nr, args); \
213 if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (resultvar, ))) \
215 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
216 resultvar = (unsigned long int) -1; \
218 (long int) resultvar; })
220 # undef INTERNAL_SYSCALL_DECL
221 # define INTERNAL_SYSCALL_DECL(err) do { } while (0)
223 /* Registers clobbered by syscall. */
224 # define REGISTERS_CLOBBERED_BY_SYSCALL "cc", "r11", "cx"
226 /* Create a variable 'name' based on type 'X' to avoid explicit types.
227 This is mainly used set use 64-bits arguments in x32. */
228 #define TYPEFY(X, name) __typeof__ ((X) - (X)) name
229 /* Explicit cast the argument to avoid integer from pointer warning on
230 x32. */
231 #define ARGIFY(X) ((__typeof__ ((X) - (X))) (X))
233 #undef INTERNAL_SYSCALL
234 #define INTERNAL_SYSCALL(name, err, nr, args...) \
235 internal_syscall##nr (SYS_ify (name), err, args)
237 #undef INTERNAL_SYSCALL_NCS
238 #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
239 internal_syscall##nr (number, err, args)
241 #undef internal_syscall0
242 #define internal_syscall0(number, err, dummy...) \
243 ({ \
244 unsigned long int resultvar; \
245 asm volatile ( \
246 "syscall\n\t" \
247 : "=a" (resultvar) \
248 : "0" (number) \
249 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
250 (long int) resultvar; \
253 #undef internal_syscall1
254 #define internal_syscall1(number, err, arg1) \
255 ({ \
256 unsigned long int resultvar; \
257 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
258 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
259 asm volatile ( \
260 "syscall\n\t" \
261 : "=a" (resultvar) \
262 : "0" (number), "r" (_a1) \
263 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
264 (long int) resultvar; \
267 #undef internal_syscall2
268 #define internal_syscall2(number, err, arg1, arg2) \
269 ({ \
270 unsigned long int resultvar; \
271 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
272 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
273 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
274 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
275 asm volatile ( \
276 "syscall\n\t" \
277 : "=a" (resultvar) \
278 : "0" (number), "r" (_a1), "r" (_a2) \
279 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
280 (long int) resultvar; \
283 #undef internal_syscall3
284 #define internal_syscall3(number, err, arg1, arg2, arg3) \
285 ({ \
286 unsigned long int resultvar; \
287 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
288 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
289 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
290 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
291 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
292 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
293 asm volatile ( \
294 "syscall\n\t" \
295 : "=a" (resultvar) \
296 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3) \
297 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
298 (long int) resultvar; \
301 #undef internal_syscall4
302 #define internal_syscall4(number, err, arg1, arg2, arg3, arg4) \
303 ({ \
304 unsigned long int resultvar; \
305 TYPEFY (arg4, __arg4) = ARGIFY (arg4); \
306 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
307 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
308 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
309 register TYPEFY (arg4, _a4) asm ("r10") = __arg4; \
310 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
311 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
312 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
313 asm volatile ( \
314 "syscall\n\t" \
315 : "=a" (resultvar) \
316 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4) \
317 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
318 (long int) resultvar; \
321 #undef internal_syscall5
322 #define internal_syscall5(number, err, arg1, arg2, arg3, arg4, arg5) \
323 ({ \
324 unsigned long int resultvar; \
325 TYPEFY (arg5, __arg5) = ARGIFY (arg5); \
326 TYPEFY (arg4, __arg4) = ARGIFY (arg4); \
327 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
328 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
329 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
330 register TYPEFY (arg5, _a5) asm ("r8") = __arg5; \
331 register TYPEFY (arg4, _a4) asm ("r10") = __arg4; \
332 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
333 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
334 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
335 asm volatile ( \
336 "syscall\n\t" \
337 : "=a" (resultvar) \
338 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \
339 "r" (_a5) \
340 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
341 (long int) resultvar; \
344 #undef internal_syscall6
345 #define internal_syscall6(number, err, arg1, arg2, arg3, arg4, arg5, arg6) \
346 ({ \
347 unsigned long int resultvar; \
348 TYPEFY (arg6, __arg6) = ARGIFY (arg6); \
349 TYPEFY (arg5, __arg5) = ARGIFY (arg5); \
350 TYPEFY (arg4, __arg4) = ARGIFY (arg4); \
351 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
352 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
353 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
354 register TYPEFY (arg6, _a6) asm ("r9") = __arg6; \
355 register TYPEFY (arg5, _a5) asm ("r8") = __arg5; \
356 register TYPEFY (arg4, _a4) asm ("r10") = __arg4; \
357 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
358 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
359 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
360 asm volatile ( \
361 "syscall\n\t" \
362 : "=a" (resultvar) \
363 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \
364 "r" (_a5), "r" (_a6) \
365 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
366 (long int) resultvar; \
369 # undef INTERNAL_SYSCALL_ERROR_P
370 # define INTERNAL_SYSCALL_ERROR_P(val, err) \
371 ((unsigned long int) (long int) (val) >= -4095L)
373 # undef INTERNAL_SYSCALL_ERRNO
374 # define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
376 /* List of system calls which are supported as vsyscalls. */
377 # define HAVE_CLOCK_GETTIME_VSYSCALL 1
378 # define HAVE_GETTIMEOFDAY_VSYSCALL 1
379 # define HAVE_GETCPU_VSYSCALL 1
381 # define SINGLE_THREAD_BY_GLOBAL 1
383 #endif /* __ASSEMBLER__ */
386 /* Pointer mangling support. */
387 #if IS_IN (rtld)
388 /* We cannot use the thread descriptor because in ld.so we use setjmp
389 earlier than the descriptor is initialized. */
390 # ifdef __ASSEMBLER__
391 # define PTR_MANGLE(reg) xor __pointer_chk_guard_local(%rip), reg; \
392 rol $2*LP_SIZE+1, reg
393 # define PTR_DEMANGLE(reg) ror $2*LP_SIZE+1, reg; \
394 xor __pointer_chk_guard_local(%rip), reg
395 # else
396 # define PTR_MANGLE(reg) asm ("xor __pointer_chk_guard_local(%%rip), %0\n" \
397 "rol $2*" LP_SIZE "+1, %0" \
398 : "=r" (reg) : "0" (reg))
399 # define PTR_DEMANGLE(reg) asm ("ror $2*" LP_SIZE "+1, %0\n" \
400 "xor __pointer_chk_guard_local(%%rip), %0" \
401 : "=r" (reg) : "0" (reg))
402 # endif
403 #else
404 # ifdef __ASSEMBLER__
405 # define PTR_MANGLE(reg) xor %fs:POINTER_GUARD, reg; \
406 rol $2*LP_SIZE+1, reg
407 # define PTR_DEMANGLE(reg) ror $2*LP_SIZE+1, reg; \
408 xor %fs:POINTER_GUARD, reg
409 # else
410 # define PTR_MANGLE(var) asm ("xor %%fs:%c2, %0\n" \
411 "rol $2*" LP_SIZE "+1, %0" \
412 : "=r" (var) \
413 : "0" (var), \
414 "i" (offsetof (tcbhead_t, \
415 pointer_guard)))
416 # define PTR_DEMANGLE(var) asm ("ror $2*" LP_SIZE "+1, %0\n" \
417 "xor %%fs:%c2, %0" \
418 : "=r" (var) \
419 : "0" (var), \
420 "i" (offsetof (tcbhead_t, \
421 pointer_guard)))
422 # endif
423 #endif
425 /* How to pass the off{64}_t argument on p{readv,writev}{64}. */
426 #undef LO_HI_LONG
427 #define LO_HI_LONG(val) (val), 0
429 /* Each shadow stack slot takes 8 bytes. Assuming that each stack
430 frame takes 256 bytes, this is used to compute shadow stack size
431 from stack size. */
432 #define STACK_SIZE_TO_SHADOW_STACK_SIZE_SHIFT 5
434 #endif /* linux/x86_64/sysdep.h */