]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/x86_64/sysdep.h
Remove PREPARE_VERSION and PREPARE_VERSION_KNOW
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / x86_64 / sysdep.h
CommitLineData
04277e02 1/* Copyright (C) 2001-2019 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
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
c9cf6dde
AJ
17
18#ifndef _LINUX_X86_64_SYSDEP_H
19#define _LINUX_X86_64_SYSDEP_H 1
20
21/* There is some commonality. */
fb1cf108 22#include <sysdeps/unix/sysv/linux/sysdep.h>
c9cf6dde 23#include <sysdeps/unix/x86_64/sysdep.h>
89b91a92 24#include <tls.h>
c9cf6dde 25
dd28d4ba
ST
26/* Defines RTLD_PRIVATE_ERRNO. */
27#include <dl-sysdep.h>
ce460d04 28
c9cf6dde
AJ
29/* For Linux we can use the system call table in the header file
30 /usr/include/asm/unistd.h
31 of the kernel. But these symbols do not follow the SYS_* syntax
32 so we have to redefine the `SYS_ify' macro here. */
33#undef SYS_ify
34#define SYS_ify(syscall_name) __NR_##syscall_name
35
3899afcb
RM
36/* This is a kludge to make syscalls.list find these under the names
37 pread and pwrite, since some kernel headers define those names
38 and some define the *64 names for the same system calls. */
39#if !defined __NR_pread && defined __NR_pread64
40# define __NR_pread __NR_pread64
41#endif
42#if !defined __NR_pwrite && defined __NR_pwrite64
43# define __NR_pwrite __NR_pwrite64
44#endif
45
0fe8d5e6
AJ
46/* This is to help the old kernel headers where __NR_semtimedop is not
47 available. */
48#ifndef __NR_semtimedop
49# define __NR_semtimedop 220
50#endif
51
52
c9cf6dde
AJ
53#ifdef __ASSEMBLER__
54
55/* Linux uses a negative return value to indicate syscall errors,
56 unlike most Unices, which use the condition codes' carry flag.
57
58 Since version 2.1 the return value of a system call might be
59 negative even if the call succeeded. E.g., the `lseek' system call
60 might return a large offset. Therefore we must not anymore test
61 for < 0, but test for a real error by making sure the value in %eax
62 is a real error number. Linus said he will make sure the no syscall
63 returns a value in -1 .. -4095 as a valid result so we can savely
64 test with -4095. */
65
66/* We don't want the label for the error handle to be global when we define
67 it here. */
75fb247e
UD
68# ifdef PIC
69# define SYSCALL_ERROR_LABEL 0f
70# else
71# define SYSCALL_ERROR_LABEL syscall_error
72# endif
c9cf6dde 73
75fb247e
UD
74# undef PSEUDO
75# define PSEUDO(name, syscall_name, args) \
c9cf6dde
AJ
76 .text; \
77 ENTRY (name) \
ffa8d2a0 78 DO_CALL (syscall_name, args); \
c9cf6dde 79 cmpq $-4095, %rax; \
842a39cd 80 jae SYSCALL_ERROR_LABEL
c9cf6dde 81
75fb247e
UD
82# undef PSEUDO_END
83# define PSEUDO_END(name) \
c9cf6dde
AJ
84 SYSCALL_ERROR_HANDLER \
85 END (name)
86
75fb247e
UD
87# undef PSEUDO_NOERRNO
88# define PSEUDO_NOERRNO(name, syscall_name, args) \
9eb88290
UD
89 .text; \
90 ENTRY (name) \
91 DO_CALL (syscall_name, args)
92
75fb247e
UD
93# undef PSEUDO_END_NOERRNO
94# define PSEUDO_END_NOERRNO(name) \
9eb88290
UD
95 END (name)
96
75fb247e 97# define ret_NOERRNO ret
f38afd78 98
75fb247e
UD
99# undef PSEUDO_ERRVAL
100# define PSEUDO_ERRVAL(name, syscall_name, args) \
bfef9264
UD
101 .text; \
102 ENTRY (name) \
103 DO_CALL (syscall_name, args); \
f38afd78 104 negq %rax
bfef9264 105
75fb247e
UD
106# undef PSEUDO_END_ERRVAL
107# define PSEUDO_END_ERRVAL(name) \
bfef9264
UD
108 END (name)
109
75fb247e 110# define ret_ERRVAL ret
9eb88290 111
dd28d4ba 112# if defined PIC && RTLD_PRIVATE_ERRNO
bbb3154b
L
113# define SYSCALL_SET_ERRNO \
114 lea rtld_errno(%rip), %RCX_LP; \
0e44a77e
L
115 neg %eax; \
116 movl %eax, (%rcx)
d063d164 117# else
4f41c682 118# if IS_IN (libc)
75fb247e
UD
119# define SYSCALL_ERROR_ERRNO __libc_errno
120# else
121# define SYSCALL_ERROR_ERRNO errno
122# endif
bbb3154b 123# define SYSCALL_SET_ERRNO \
0a951d0e 124 movq SYSCALL_ERROR_ERRNO@GOTTPOFF(%rip), %rcx;\
0e44a77e
L
125 neg %eax; \
126 movl %eax, %fs:(%rcx);
bbb3154b
L
127# endif
128
129# ifndef PIC
130# define SYSCALL_ERROR_HANDLER /* Nothing here; code in sysdep.S is used. */
131# else
132# define SYSCALL_ERROR_HANDLER \
1330: \
134 SYSCALL_SET_ERRNO; \
135 or $-1, %RAX_LP; \
5e292e4f 136 ret;
75fb247e 137# endif /* PIC */
c9cf6dde 138
092fd00d
AJ
139/* The Linux/x86-64 kernel expects the system call parameters in
140 registers according to the following table:
c9cf6dde 141
092fd00d 142 syscall number rax
c9cf6dde
AJ
143 arg 1 rdi
144 arg 2 rsi
145 arg 3 rdx
092fd00d 146 arg 4 r10
c9cf6dde
AJ
147 arg 5 r8
148 arg 6 r9
149
092fd00d 150 The Linux kernel uses and destroys internally these registers:
c9cf6dde
AJ
151 return address from
152 syscall rcx
c9cf6dde
AJ
153 eflags from syscall r11
154
092fd00d
AJ
155 Normal function call, including calls to the system call stub
156 functions in the libc, get the first six parameters passed in
157 registers and the seventh parameter and later on the stack. The
158 register use is as follows:
159
160 system call number in the DO_CALL macro
161 arg 1 rdi
162 arg 2 rsi
163 arg 3 rdx
164 arg 4 rcx
165 arg 5 r8
166 arg 6 r9
167
168 We have to take care that the stack is aligned to 16 bytes. When
169 called the stack is not aligned since the return address has just
170 been pushed.
171
9eb88290 172
092fd00d 173 Syscalls of more than 6 arguments are not supported. */
c9cf6dde 174
75fb247e
UD
175# undef DO_CALL
176# define DO_CALL(syscall_name, args) \
c9cf6dde 177 DOARGS_##args \
ee618985 178 movl $SYS_ify (syscall_name), %eax; \
c9cf6dde
AJ
179 syscall;
180
75fb247e
UD
181# define DOARGS_0 /* nothing */
182# define DOARGS_1 /* nothing */
183# define DOARGS_2 /* nothing */
184# define DOARGS_3 /* nothing */
185# define DOARGS_4 movq %rcx, %r10;
186# define DOARGS_5 DOARGS_4
187# define DOARGS_6 DOARGS_5
c9cf6dde
AJ
188
189#else /* !__ASSEMBLER__ */
190/* Define a macro which expands inline into the wrapper code for a system
191 call. */
75fb247e
UD
192# undef INLINE_SYSCALL
193# define INLINE_SYSCALL(name, nr, args...) \
369b849f 194 ({ \
78b767a0 195 unsigned long int resultvar = INTERNAL_SYSCALL (name, , nr, args); \
a1ffb40e 196 if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (resultvar, ))) \
369b849f 197 { \
6aca81bb 198 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
78b767a0 199 resultvar = (unsigned long int) -1; \
369b849f 200 } \
78b767a0 201 (long int) resultvar; })
369b849f 202
31a39bd8
L
203/* Define a macro with explicit types for arguments, which expands inline
204 into the wrapper code for a system call. It should be used when size
205 of any argument > size of long int. */
206# undef INLINE_SYSCALL_TYPES
207# define INLINE_SYSCALL_TYPES(name, nr, args...) \
208 ({ \
209 unsigned long int resultvar = INTERNAL_SYSCALL_TYPES (name, , nr, args); \
a1ffb40e 210 if (__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (resultvar, ))) \
31a39bd8
L
211 { \
212 __set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, )); \
213 resultvar = (unsigned long int) -1; \
214 } \
215 (long int) resultvar; })
216
75fb247e
UD
217# undef INTERNAL_SYSCALL_DECL
218# define INTERNAL_SYSCALL_DECL(err) do { } while (0)
6aca81bb 219
d29d57ee
L
220/* Registers clobbered by syscall. */
221# define REGISTERS_CLOBBERED_BY_SYSCALL "cc", "r11", "cx"
222
78ca091c
AZ
223/* Create a variable 'name' based on type 'X' to avoid explicit types.
224 This is mainly used set use 64-bits arguments in x32. */
225#define TYPEFY(X, name) __typeof__ ((X) - (X)) name
226/* Explicit cast the argument to avoid integer from pointer warning on
227 x32. */
228#define ARGIFY(X) ((__typeof__ ((X) - (X))) (X))
229
230#undef INTERNAL_SYSCALL
231#define INTERNAL_SYSCALL(name, err, nr, args...) \
232 internal_syscall##nr (SYS_ify (name), err, args)
233
234#undef INTERNAL_SYSCALL_NCS
235#define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
236 internal_syscall##nr (number, err, args)
237
238#undef internal_syscall0
239#define internal_syscall0(number, err, dummy...) \
240({ \
241 unsigned long int resultvar; \
242 asm volatile ( \
243 "syscall\n\t" \
244 : "=a" (resultvar) \
245 : "0" (number) \
246 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
247 (long int) resultvar; \
248})
249
250#undef internal_syscall1
251#define internal_syscall1(number, err, arg1) \
252({ \
253 unsigned long int resultvar; \
254 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
255 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
256 asm volatile ( \
257 "syscall\n\t" \
258 : "=a" (resultvar) \
259 : "0" (number), "r" (_a1) \
260 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
261 (long int) resultvar; \
262})
263
264#undef internal_syscall2
265#define internal_syscall2(number, err, arg1, arg2) \
266({ \
267 unsigned long int resultvar; \
268 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
269 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
270 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
271 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
272 asm volatile ( \
273 "syscall\n\t" \
274 : "=a" (resultvar) \
275 : "0" (number), "r" (_a1), "r" (_a2) \
276 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
277 (long int) resultvar; \
278})
279
280#undef internal_syscall3
281#define internal_syscall3(number, err, arg1, arg2, arg3) \
282({ \
283 unsigned long int resultvar; \
284 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
285 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
286 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
287 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
288 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
289 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
290 asm volatile ( \
291 "syscall\n\t" \
292 : "=a" (resultvar) \
293 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3) \
294 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
295 (long int) resultvar; \
296})
297
298#undef internal_syscall4
299#define internal_syscall4(number, err, arg1, arg2, arg3, arg4) \
300({ \
301 unsigned long int resultvar; \
302 TYPEFY (arg4, __arg4) = ARGIFY (arg4); \
303 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
304 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
305 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
306 register TYPEFY (arg4, _a4) asm ("r10") = __arg4; \
307 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
308 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
309 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
310 asm volatile ( \
311 "syscall\n\t" \
312 : "=a" (resultvar) \
313 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4) \
314 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
315 (long int) resultvar; \
316})
317
318#undef internal_syscall5
319#define internal_syscall5(number, err, arg1, arg2, arg3, arg4, arg5) \
320({ \
321 unsigned long int resultvar; \
322 TYPEFY (arg5, __arg5) = ARGIFY (arg5); \
323 TYPEFY (arg4, __arg4) = ARGIFY (arg4); \
324 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
325 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
326 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
327 register TYPEFY (arg5, _a5) asm ("r8") = __arg5; \
328 register TYPEFY (arg4, _a4) asm ("r10") = __arg4; \
329 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
330 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
331 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
332 asm volatile ( \
333 "syscall\n\t" \
334 : "=a" (resultvar) \
335 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \
336 "r" (_a5) \
337 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
338 (long int) resultvar; \
339})
340
341#undef internal_syscall6
342#define internal_syscall6(number, err, arg1, arg2, arg3, arg4, arg5, arg6) \
343({ \
344 unsigned long int resultvar; \
345 TYPEFY (arg6, __arg6) = ARGIFY (arg6); \
346 TYPEFY (arg5, __arg5) = ARGIFY (arg5); \
347 TYPEFY (arg4, __arg4) = ARGIFY (arg4); \
348 TYPEFY (arg3, __arg3) = ARGIFY (arg3); \
349 TYPEFY (arg2, __arg2) = ARGIFY (arg2); \
350 TYPEFY (arg1, __arg1) = ARGIFY (arg1); \
351 register TYPEFY (arg6, _a6) asm ("r9") = __arg6; \
352 register TYPEFY (arg5, _a5) asm ("r8") = __arg5; \
353 register TYPEFY (arg4, _a4) asm ("r10") = __arg4; \
354 register TYPEFY (arg3, _a3) asm ("rdx") = __arg3; \
355 register TYPEFY (arg2, _a2) asm ("rsi") = __arg2; \
356 register TYPEFY (arg1, _a1) asm ("rdi") = __arg1; \
357 asm volatile ( \
358 "syscall\n\t" \
359 : "=a" (resultvar) \
360 : "0" (number), "r" (_a1), "r" (_a2), "r" (_a3), "r" (_a4), \
361 "r" (_a5), "r" (_a6) \
362 : "memory", REGISTERS_CLOBBERED_BY_SYSCALL); \
363 (long int) resultvar; \
364})
31a39bd8 365
75fb247e
UD
366# undef INTERNAL_SYSCALL_ERROR_P
367# define INTERNAL_SYSCALL_ERROR_P(val, err) \
78b767a0 368 ((unsigned long int) (long int) (val) >= -4095L)
369b849f 369
75fb247e
UD
370# undef INTERNAL_SYSCALL_ERRNO
371# define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
372
b8386c28
AZ
373# define VDSO_NAME "LINUX_2.6"
374# define VDSO_HASH 61765110
375
1395ef21
AZ
376/* List of system calls which are supported as vsyscalls. */
377# define HAVE_CLOCK_GETTIME_VSYSCALL 1
378# define HAVE_GETTIMEOFDAY_VSYSCALL 1
dd26c444 379# define HAVE_GETCPU_VSYSCALL 1
1395ef21 380
09c76a74
AZ
381# define SINGLE_THREAD_BY_GLOBAL 1
382
c9cf6dde
AJ
383#endif /* __ASSEMBLER__ */
384
827b7087
UD
385
386/* Pointer mangling support. */
a3848485 387#if IS_IN (rtld)
827b7087
UD
388/* We cannot use the thread descriptor because in ld.so we use setjmp
389 earlier than the descriptor is initialized. */
3467f5c3 390# ifdef __ASSEMBLER__
e02f153a
L
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
3467f5c3 395# else
e02f153a
L
396# define PTR_MANGLE(reg) asm ("xor __pointer_chk_guard_local(%%rip), %0\n" \
397 "rol $2*" LP_SIZE "+1, %0" \
4a44ce79 398 : "=r" (reg) : "0" (reg))
e02f153a
L
399# define PTR_DEMANGLE(reg) asm ("ror $2*" LP_SIZE "+1, %0\n" \
400 "xor __pointer_chk_guard_local(%%rip), %0" \
7725f874 401 : "=r" (reg) : "0" (reg))
3467f5c3 402# endif
827b7087 403#else
3467f5c3 404# ifdef __ASSEMBLER__
e02f153a
L
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
3467f5c3 409# else
e02f153a
L
410# define PTR_MANGLE(var) asm ("xor %%fs:%c2, %0\n" \
411 "rol $2*" LP_SIZE "+1, %0" \
4a44ce79
UD
412 : "=r" (var) \
413 : "0" (var), \
414 "i" (offsetof (tcbhead_t, \
415 pointer_guard)))
e02f153a
L
416# define PTR_DEMANGLE(var) asm ("ror $2*" LP_SIZE "+1, %0\n" \
417 "xor %%fs:%c2, %0" \
3467f5c3
UD
418 : "=r" (var) \
419 : "0" (var), \
420 "i" (offsetof (tcbhead_t, \
421 pointer_guard)))
3467f5c3 422# endif
827b7087
UD
423#endif
424
cf1ad5b3
L
425/* How to pass the off{64}_t argument on p{readv,writev}{64}. */
426#undef LO_HI_LONG
d8a7d103 427#define LO_HI_LONG(val) (val), 0
cf1ad5b3 428
25123a1c
L
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
433
c9cf6dde 434#endif /* linux/x86_64/sysdep.h */